Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. There is only ViewData.ModelMetadata.

like image 958
user137348 Avatar asked Dec 28 '22 20:12

user137348


2 Answers

Can you try

<% foreach (var item in Model.MyCollection) { %>
    <%= html.EditorFor(m=>item) %>
<% } %>
like image 73
Allen Wang Avatar answered Dec 31 '22 10:12

Allen Wang


Something like this, in your view?

<% foreach (var item in Model.MyCollection) { %>
    <%= html.EditorFor... %>
    ...
<% } %>

See also using Html.EditorFor with an IEnumerable<T>

like image 21
Robert Harvey Avatar answered Dec 31 '22 10:12

Robert Harvey