Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 generation of the List/Index view

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?

like image 877
Klas Mellbourn Avatar asked Mar 23 '10 14:03

Klas Mellbourn


2 Answers

i think that Phil Haack 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...

like image 190
moi_meme Avatar answered Dec 05 '22 07:12

moi_meme


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

like image 34
Jack128 Avatar answered Dec 05 '22 07:12

Jack128