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

白昼怎懂夜的黑 提交于 2019-11-27 13:53: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) %>

<% } %>

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);
   } %>
T-moty

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

Anders E. Andersen

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.

Hovhannes Babayan

In Razor I am using

@Html.DisplayFor(model => item.CreatedDate)

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!