views:

55

answers:

2

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.DisplayFor() function:

<@ Page Inherits="ViewPage<IEnumerable<Foo>>">

<% foreach(var item in Model) { %>

    <%: Html.DisplayFor(item.BarBaz) %>

<% } %>

I need to be able to use the DisplayTemplate for this value. Is there a way to do this?

+2  A: 

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);
   } %>
sgriffinusa
i like option 2 better
Necros
+1 but I'm with Necros / option 2 is much cleaner
Tahbaza
I agree that option 2 is a better solution. I just included option 1 as I thought of it first and didn't really have any reason to remove it as it does solve the problem.
sgriffinusa
+2  A: 

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) %>

<% } %>
John Gietzen