MVC4 Model ValueProvider

Posted dragon.net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MVC4 Model ValueProvider相关的知识,希望对你有一定的参考价值。

1. NameValueCollectionValueProvider: 

   ValueProvider 的数据容器一般具有类似字典的结构。NameValueCollection 表示一种 key 和value 均为字符的字典。

  方法 GetKeysFromPrefix  以字典的形式返回数据源容器中所有具有指定前缀的key。

   两种前缀的形式:

  两种前缀形式辅助实现Model绑定数据。

  一种是采用"." 一种是采用"[]" 集合的方式:

        public ActionResult Index()
        { 
            NameValueCollection datasource = new NameValueCollection();

            datasource.Add("foo.Name", "Foo");
            datasource.Add("foo.PhoneNo", "123456789");
            datasource.Add("foo.EmailAddress", "[email protected]");

            datasource.Add("foo.Address.Province", "江苏");
            datasource.Add("foo.Address.City", "苏州");
            datasource.Add("foo.Address.District", "工业园区");
            datasource.Add("foo.Address.Street", "星湖街328号");

            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(datasource, CultureInfo.InvariantCulture);
            return View(valueProvider);
        }
@model NameValueCollectionValueProvider
<html>
<head>
    <title>指定前缀的Key</title>
    <link rel="stylesheet"  href="~/Style.css" />
</head>
<body>
    <table rules="all">
        <tr><th colspan="2">foo</th></tr>
        @foreach (var item in Model.GetKeysFromPrefix("foo"))
        { 
            <tr><td>@item.Key</td><td>@item.Value</td></tr>
        }

        <tr><th colspan="2">foo.Address</th></tr>
        @foreach (var item in Model.GetKeysFromPrefix("foo.Address"))
        { 
            <tr><td>@item.Key</td><td>@item.Value</td></tr>
        }
    </table>
</body>
</html>

 

现在来看看数组"[]"形式的代码:

        public ActionResult Index()
        {
            NameValueCollection datasource = new NameValueCollection();

            datasource.Add("first[0].Name", "Foo");
            datasource.Add("first[0].PhoneNo", "123456789");
            datasource.Add("first[0].EmailAddress", "[email protected]");

            datasource.Add("first[1].Name", "Bar");
            datasource.Add("first[1].PhoneNo", "987654321");
            datasource.Add("first[1].EmailAddress", "[email protected]");

            NameValueCollectionValueProvider valueProvider = new NameValueCollectionValueProvider(datasource, CultureInfo.InvariantCulture);

            return View(valueProvider);
        }
@model NameValueCollectionValueProvider
<html>
<head>
    <title>指定前缀的Key</title>
    <link rel="stylesheet"  href="~/Style.css" />
</head>
<body>
    <table>
        <tr><th colspan="2">first</th></tr>
        @foreach (var item in Model.GetKeysFromPrefix("first"))
        { 
            <tr><td>@item.Key</td><td>@item.Value</td></tr>
        }

        <tr><th colspan="2">first[0]</th></tr>
        @foreach (var item in Model.GetKeysFromPrefix("first[0]"))
        { 
            <tr><td>@item.Key</td><td>@item.Value</td></tr>
        }

        <tr><th colspan="2">first[1]</th></tr>
        @foreach (var item in Model.GetKeysFromPrefix("first[1]"))
        { 
            <tr><td>@item.Key</td><td>@item.Value</td></tr>
        }
    </table>

</body>
</html>

 

2. DictionaryValueProvider: 是将数据源存放在真正的字典对象之中。他们之间不同之处在于NameValueCollection 中的元素仅局限于字符串。

        public ActionResult DataOfChildActionValueProvider()
        {
            ControllerContext.RouteData.Values["Foo"] = "abc";
            ControllerContext.RouteData.Values["Bar"] = "ijk";
            ControllerContext.RouteData.Values["Baz"] = "xyz";

            ChildActionValueProvider valueProvider = new ChildActionValueProvider(ControllerContext);
            return View(valueProvider);
        }

 

@model ChildActionValueProvider
<table rules="all">
    <tr>
        <th>Key</th><th colspan="2">Value</th>
    </tr>
    @{
        var dictionary1 = this.Model.GetDataSource();   
    }
    @foreach (var item1 in dictionary1)
    {

        DictionaryValueProvider<object> valueProvider = item1.Value.RawValue as DictionaryValueProvider<object>;
        if (null == valueProvider)
        { 
            <tr>
                <td>@item1.Key</td><td colspan="2">@item1.Value.RawValue</td>
            </tr>
        }
        else
        {
            var dictionary2 = valueProvider.GetDataSource();
            <tr>
                <td rowspan="@(dictionary2.Count + 1)">@item1.Key</td>
                <th>Key</th><th>Value</th>
            </tr>
            foreach(var item2 in dictionary2)
            {
                <tr><td>@item2.Key</td><td>@item2.Value.RawValue</td></tr>
            }            
        }
    }
</table>

同时在Index 中

<html>
<head>
    <title>ChildActionValueProvider的数据结构</title>
    <link rel="stylesheet"  href="~/Style.css" />
</head>
<body>
    @Html.Action("DataOfChildActionValueProvider", new { Foo = 123, Bar = 456, Baz = 789 })
</body>
</html>

 

以上是关于MVC4 Model ValueProvider的主要内容,如果未能解决你的问题,请参考以下文章

MVC4 不会将模型传递给控制器

MVC4方法行为过滤器例子(用户登录)

Asp.Net Mvc4 WebSecurity 如何激活或停用用户帐户

AspNet MVC4 教学-25:Asp.Net MVC4 强弱类型View等技术高速对照Demo

如何在 mvc4 razor 中显示验证消息

MVC4学习笔记