开发者

Model Binding a Dictionary

My controller action method passes a Dictionary<string, double?> to the view. I have the following in my view:

<% foreach (var item in Model.Items) { %>
<%: Html.Label(item.Key, item.Key)%>
<%: Html.TextBox(item.Key, item.Value)%>
<% } %>

Below is my action method which handles the POST operation:

[HttpPost]
public virtual ActionResult MyMethod(Dictionary<string, double?> items)
{
    // do st开发者_运维知识库uff........
    return View();
}

When I enter the some values into the textbox and hit the submit button the POST action method is not getting any items back? What am I doing wrong?


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) %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜