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", "Foo@gmail.com");

            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", "Foo@gmail.com");

            datasource.Add("first[1].Name", "Bar");
            datasource.Add("first[1].PhoneNo", "987654321");
            datasource.Add("first[1].EmailAddress", "Bar@gmail.com");

            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>
原文地址:https://www.cnblogs.com/dragon-L/p/5246912.html