0

I have the following ViewModel on an ASP.NET MVC:

public CategoryNewViewModel {
  public Int32 Type { get ; set; }
  public IDictionary<String, String> Descriptions { get; set; }
}

The Description Key is the language and Description Value is the text.

On the view I will need to render N inputs, one for each language ...

When the form is submitted the Descriptions property would become:

"en", "The description in english"
"pt", "A descrição em português"
"fr", "La description en français"

One problem is I am not sure how many inputs I will have on the view.

Does anyone knows if this binding is possible?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

1 Answers1

0

I think i find nice way of binding dictionary and it's actually what you need in your situation.

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" />
}

Stolen from this question =)

Community
  • 1
  • 1
teo van kot
  • 12,350
  • 10
  • 38
  • 70