Model Binding a Dictionary

十年热恋 提交于 2019-11-30 20:27:21

I would recommend you reading this blog post about how your input fields should be named so that you can bind to a dictionary. So you will need an additional hidden field for the key:

<input type="hidden" name="items[0].Key" value="key1" />
<input type="text" name="items[0].Value" value="15.4" />
<input type="hidden" name="items[1].Key" value="key2" />
<input type="text" name="items[1].Value" value="17.8" />

which could be generated with something along the lines:

<% var index = 0; %>
<% foreach (var key in Model.Keys) { %>
    <%: Html.Hidden("items[" + index + "].Key", key) %>
    <%: Html.TextBox("items[" + index +"].Value", Model[key]) %>
    <% index++; %>
<% } %>

This being said, personally I would recommend you NOT using dictionaries in your views. They are ugly and in order to generate proper names for the model binder you need to write ugly code. I would use view models. Here's an example:

Model:

public class MyViewModel
{
    public string Key { get; set; }
    public double? Value { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new[]
        {
            new MyViewModel { Key = "key1", Value = 15.4 },
            new MyViewModel { Key = "key2", Value = 16.1 },
            new MyViewModel { Key = "key3", Value = 20 },
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<MyViewModel> items)
    {
        return View(items);
    }
}

View (~/Views/Home/Index.aspx):

<% using (Html.BeginForm()) { %>
    <%: Html.EditorForModel() %>
    <input type="submit" value="OK" />
<% } %>

Editor Template (~/Views/Home/EditorTemplates/MyViewModel.ascx):

<%@ Control 
    Language="C#"
    Inherits="System.Web.Mvc.ViewUserControl<Models.MyViewModel>" %>
<%: Html.HiddenFor(x => x.Key) %>
<%: Html.TextBoxFor(x => x.Value) %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!