开发者

ASP.NET MVC Binding to a dictionary

开发者 https://www.devze.com 2023-02-15 05:57 出处:网络
I\'m trying to bind dictionary values within MVC. Within the action I have: model.Params = new Dictionary<string, string>();

I'm trying to bind dictionary values within MVC.

Within the action I have:

model.Params = new Dictionary<string, string>();

model.Params.Add("Value1", "1");
model.Params.Add("Value2", "2");
model.Params.Add("Value3", "3");

and within the view I have:

@foreach (KeyValuePair<string, string> kvp in Model.Params)
{ 
<tr>
  <td>
    <input type="hidden" name="Params.Key" value="@kvp.Key" />
    @Html.TextBox("Params[" + kvp.Key + "]")
  </t开发者_开发技巧d>
</tr>
}

But the view doesn't display the initial values, and when the form is submitted the Params property is null?


In ASP.NET MVC 4, the default model binder will bind dictionaries using the typical dictionary indexer syntax property[key].

If you have a Dictionary<string, string> in your model, you can now bind back to it directly with the following markup:

<input type="hidden" name="MyDictionary[MyKey]" value="MyValue" />

For example, if you want to use a set of checkboxes to select a subset of a dictionary's elements and bind back to the same property, you can do the following:

@foreach(var kvp in Model.MyDictionary)
{
    <input type="checkbox" name="@("MyDictionary[" + kvp.Key + "]")"
        value="@kvp.Value" />
}


you should take a look to this post from scott hanselman: http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx

The default binder just understand dictionaries in the format:

params[0].key = kvp.key
params[0].value = kvp.value

The index of the param must be sequential, starting from 0 and without any gaps. The current helpers don't support this, so you should create the form input fields by yourself.

you can of course implement your own binder, like this one: http://siphon9.net/loune/2009/12/a-intuitive-dictionary-model-binder-for-asp-net-mvc/


Building on @AntP's answer, there is an even less verbose way, letting MVC do more of the work (at least with TextBoxFor() on a Dictionary<string, string> - I haven't tried CheckBoxFor() on a Dictionary<xxx, bool>):

@foreach(var kvp in Model.MyDictionary)
{
    @Html.Label(kvp.Key);
    @Html.TextBoxFor(m => m.MyDictionary[kvp.Key]);
}
0

精彩评论

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