I have the following code in my view
<% foreach (var item in Model.stats)
{%>
<label style="style="float:left;"><%= item.Stat_Caption %></label>
<%=Html.TextBox(item.Stat_Caption,item.Stat_Value) %>
<%} %>
I’m trying to turn my stats object which is just a list collection, into a list of textboxes so the user can update them.
which i have got working, how do i once the user updates the textboxes apply the values back to the list collection?
You need to wrap the textboxes in a form:
When you press the submit button, it will do a standard POST with key/value pairs like so:
On the controller side, you need to have a method that receives the POST request:
where
Entityis your data model object. The default MVC model binder uses reflection to populate your entity’s fields, so if the entity was like this:Then Box1 and Box2 will be set to the values that were sent in the POST request.
If you don’t have an entity, then you can use this:
where
collectionis a dictionary of objects. The bad thing with using this dictionary is that it’s a dictionary ofObjecttypes, so you’ll have to grab the data and cast it back to whatever type it’s supposed to be.