73

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 + "]")
  </td>
</tr>
}

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

3 Answers 3

99

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" />
}
6
  • Thanks for this, quite a nice way to bind.
    – valentin
    Oct 2, 2013 at 12:15
  • 6
    Can somebody provide any links to a more detailed answer? Tried for Dictionary<int, bool> and it didn't work out, so I had to go with the old indexer syntax.
    – Alex Klaus
    May 14, 2014 at 5:13
  • 11
    Doesn't appear to work with numeric keys, but does with strings.
    – Leniency
    Jun 16, 2015 at 18:47
  • 1
    @Ant P I have two input textboxes one for key and one for value . how do i map these ? Dec 4, 2017 at 14:56
  • How can i use this / bind the dictionary when adding the fields dynamically to html Dom with JavaScript? Ok
    – Opa114
    Mar 7, 2018 at 13:07
63

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/

28

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]);
}
5
  • 1
    CheckBoxFor would work with a Dict<string, bool> but it wouldn't work in any scenario where you want to bind back specific key/value pairs where the value is not just a bool - to do that you need to build the markup yourself (as in my example).
    – Ant P
    Dec 19, 2014 at 11:39
  • Why should this only work for booleans? I got this to work for Dict<string, string> as well, and I don't see why it shouldn't work for other data types as well.
    – Kai Weber
    May 16, 2017 at 11:55
  • @KaiWeber - This probably works for any Dictionary<X, Y> (in fact, <string, string> is what I use in my answer). However, I haven't tested it with anything else. What I think Ant P says is that it only works for typed dictionaries (Dictionary<>), and not non-generic collections like IDictionary
    – Sphinxxx
    May 16, 2017 at 19:03
  • 2
    I just tried this using a Dictionary<Enum, string>, which didn't work. If I use a Dictionary<string, string> instead, it works. Anybody has an explanation for this?
    – nkaenzig
    May 25, 2019 at 14:54
  • 2
    My experience was that the value can be anything, but the key had to be a string instead of an int. Maybe the model binder confuses it with a positional argument. Nov 28, 2019 at 8:26

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.