views:

60

answers:

1

if i have created a view model and have a partial form that is a create form for the view model. but now i want to take in a list model to my form and be able to loop through each item and depending on one of the properties append a string to teh end of the Name and id fields. or alternatively give each of the items in the list a unique name and id , all using the Html helpers. the reason i wish to do this is because the save method must be able to accept all the entities created in the form post. or should i just loop through and do all craetes and things through Jquery?

A: 

You can Model-bind to a collection. You don't need to give each item a unique Name as such, just append an index to it e.g.

<% for (int i = 0; i < Model.Products.Count; i++)
{ %>
    <%= Html.TextBox("ID", Model.Products[i].Id) %>
}%>

Then have you controller accept a IList<Product> and it will automatically populate the collection.

See Model Binding To A List on Haacked Blog for more info or ASP.NET MVC Model binding to a list.

Dan Diplo
thanks for the help, changed my solution to use this
Barry