How can I use Html.DisplayFor inside of an iterator?

前端 未结 6 1480
死守一世寂寞
死守一世寂寞 2020-12-03 06:23

I am loving MVC 2. The whole thing just fits the web so well.

There is one piece of functionality, however, that I am unable to coax out of the Html.

6条回答
  •  悲&欢浪女
    2020-12-03 07:09

    This is an old question, but i guess that someone can get benefits from my solution:

    Aspx view

    <%@ Page Inherits="ViewPage>" %>
    
    <% foreach (var item in Model) { %>
    
        <%: Html.DisplayFor(m => item) %>
    
    <% } %>
    

    Razor view

    @model IEnumerable
    
    @foreach (var item in Model)
    {
        Html.DisplayFor(m => item)
    }
    

    Since DisplayFor accepts an implicit typed lambda, we can directly indicate the instance to display in the loop.

    Finally, i'm agree with Anders E. Andersen's answer for the template usage

提交回复
热议问题