ASP.NET MVC 2 : How to call DisplayFor for each item in a collection?
I have property in my model which is a collection type (List). I'd like to call for each item in this collection Html.DisplayFor
or Html.EditorFor
. How can I do this ?
EDIT It's not a strong-typed view. It's a templated view. Th开发者_如何学JAVAere is only ViewData.ModelMetadata.
Can you try
<% foreach (var item in Model.MyCollection) { %>
<%= html.EditorFor(m=>item) %>
<% } %>
Something like this, in your view?
<% foreach (var item in Model.MyCollection) { %>
<%= html.EditorFor... %>
...
<% } %>
See also using Html.EditorFor with an IEnumerable<T>
The easiest way to do this is just add a 'SelectedItem' property to your model:
public class YourModel
{
public IEnumberable<Item> YourCollection
{
get;
}
public Item SelctedItem
{
get;
set;
}
}
Then just assign each item in the list to the selctedItem property:
<% foreach (var item in Model.YourCollection) { %>
Model.SelctedItem = item;
<%= html.EditorFor(SelctedItem) %>
...
<% } %>
精彩评论