MVC Form and Dictionary Type
Posted
by Brian Schmitt
on Stack Overflow
See other posts from Stack Overflow
or by Brian Schmitt
Published on 2010-04-02T23:03:46Z
Indexed on
2010/04/02
23:13 UTC
Read the original article
Hit count: 456
asp.net-mvc-2
|dynamic
I am attempting to use a property on my model that is of type Dictionary<string, string>
.
This dictionary is one of several other properties on the model, and I am using the dictionary as a key/value store to produce a 'dynamic' form.
E.G.:
User
Name
Settings
A
B
I want to be able to submit and save any changes to settings A and B. I can submit the form and get my posted values in all my other fields, however the dictionary gets set to null.
<%=Html.LabelFor(m => m.Name)%>
<%=Html.DisplayFor(m => m.Name)%>
<%=Html.LabelFor(m => m.Settings["A"])%>
<%=Html.TextBoxFor(m => m.Settings["A"])%>
<%=Html.LabelFor(m => m.Settings["B"])%>
<%=Html.TextBoxFor(m => m.Settings["B"])%>
Is there a better way to produce the dynamic form with a key/value pair? The actual code will be using partials (there several hundred variations), and the above is only a contrived example.
© Stack Overflow or respective owner