views:

470

answers:

3

ASP.NET MVC 2 has powerful features for generating the model-dependent content of the Edit view (using EditorForModel) and Details view (using DisplayForModel) that automatically utilizes metadata and editor (or display) templates:

<% using (Html.BeginForm()) {%>
    <%= Html.ValidationSummary(true) %>

    <fieldset>
        <legend><%= Html.LabelForModel() %></legend>

        <%= Html.EditorForModel() %>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>

<% } %>

However, I cannot find any comparable tools for the "last" step of generating the Index view (a.k.a. the List view). There I have to hard code the columns first in the row representing the headers and then inside the foreach loop:

<h2>Index</h2>

<table>
    <tr>
        <th></th>
        <th>
            ID
        </th>
        <th>
            Foo
        </th>
        <th>
            Bar
        </th>
    </tr>

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

    <tr>
        <td>
            <%= Html.ActionLink("Edit", "Edit", new { id=item.ID }) %> |
            <%= Html.ActionLink("Details", "Details", new { id=item.ID })%> |
            <%= Html.ActionLink("Delete", "Delete", new { id=item.ID })%>
        </td>
        <td>
            <%= Html.Encode(item.ID) %>
        </td>
        <td>
            <%= Html.Encode(item.Foo) %>
        </td>
        <td>
            <%= Html.Encode(String.Format("{0:g}", item.Bar)) %>
        </td>
    </tr>

<% } %>

</table>

What would be the best way to generate the columns (utlizing metadata such as HiddenInput), with the aim of making the Index view as free of model particulars as Edit and Details?

A: 

Take a look at Phil Haack's code based repeater. I think that may accomplish what you're looking for.

AlDev
As far as I understand that still has the requirement that you explicitly list all the columns. I was hoping to avoid that entirely, like EditorForModel or DisplayForModel.
Klas Mellbourn
+1  A: 

i think that Phil Haak is doing pretty much what you're looking for...

ASP.NET MVC2 templates feature is a pretty nice way to quickly scaffold objects at runtime. Be sure to read Brad Wilson’s fantastic series on this topic starting at ASP.NET MVC 2 Templates, Part 1: Introduction.

As great as this feature is, there is one template that’s conspicuously missing. ASP.NET MVC does not include a template for displaying a list of objects in a tabular format.

Check the link for more...

moi_meme
I'm sure its a typo, but its "Phil Haack"
Vivek Bernard
@Vivek Yes it was...
moi_meme
+1  A: 

May be this article http://haacked.com/archive/2010/05/05/asp-net-mvc-tabular-display-template.aspx help to you..

Jack128