ASP.NET MVC 2 generation of the List/Index view
Posted
by Klas Mellbourn
on Stack Overflow
See other posts from Stack Overflow
or by Klas Mellbourn
Published on 2010-03-23T14:20:24Z
Indexed on
2010/03/23
14:23 UTC
Read the original article
Hit count: 580
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?
© Stack Overflow or respective owner