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

前端 未结 6 1459
死守一世寂寞
死守一世寂寞 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:00

    Actually, I figured it out. How stupid of me.

    This works:

    <@ Page Inherits="ViewPage<IEnumerable<Foo>>">
    
    <% foreach(var item in Model) { %>
    
        <%: Html.DisplayFor(m => item.BarBaz) %>
    
    <% } %>
    
    0 讨论(0)
  • 2020-12-03 07:01

    You can accomplish it by getting away from the foreach and using a regular for loop:

     <% for (int i = 0; i < Model.Count(); i++) { %>
    
        <%: Html.DisplayFor(p=> p.ElementAt(i).BarBaz) %>
    
     <%} %>
    

    Another option would be to create a PartialView that took an Foo object and only displayed the information that you wanted.

    <% foreach (var item in Model)
       {
           Html.RenderPartial("FooBarBazLine", item);
       } %>
    
    0 讨论(0)
  • 2020-12-03 07:03

    Just be careful that a DisplayFor on a POST of a form doesnt actually create a field that can be referenced back in the controller. You would need to use an additional HiddenFor if you need to refer to fields in your submitted POST method.

    0 讨论(0)
  • 2020-12-03 07:05

    Html.DisplayFor can automatically iterate over collections, displaying a partial view for each element in the collection.

    The first thing you need to do is create an actual model class, with the collection being a property of the class.

    public class Bar
    {
        public IEnumerable<Foo> foo { get; set; }
    }
    

    Return this class from your controller instead of the raw collection.

    Secondly you need a display template for the Foo class. Display templates are partial views that need to be placed in the folder Views/Shared/DisplayTemplates.

    Edit: You can have them in your controller subfolder of Views as well if you want to limit the template to a particular controller. See this question for more information.

    Here is an example in razor syntax:

    @model YourNameSpace.Foo
    <p>@Model.BarBaz</p>
    

    Save it as Foo.cshtml in the DisplayTemplates folder given above.

    This template is pretty simple because it is based on your example where you are only displaying a string, but if the collection elements where a class with its own properties you could create a more elaborate template.

    Now in the original view, you can get rid of the loop entirely and just write

    @Html.DisplayFor(m => m.foo)
    

    Notice foo is the name of the property in your new model class that contains the old collection you looped over before.

    DisplayFor will automatically know that the foo property is of type (collection of) Foo and pick up the Foo.cshtml template in the DisplayTemplates folder and show it once for each element in foo.

    0 讨论(0)
  • 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<IEnumerable<Foo>>" %>
    
    <% foreach (var item in Model) { %>
    
        <%: Html.DisplayFor(m => item) %>
    
    <% } %>
    

    Razor view

    @model IEnumerable<Foo>
    
    @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

    0 讨论(0)
  • 2020-12-03 07:10

    In Razor I am using

    @Html.DisplayFor(model => item.CreatedDate)
    
    0 讨论(0)
提交回复
热议问题