At work, a debate broke out over MVC. In that debate, Chris Peterson commented that he hated the way things looked on the view pages, specifically with code executing in the view context. By way of example, he talked about the need for a for loop to display a collection, and how he didn't like that. Geoff Lane commented that Grails had support for this built-in.
To overcome these objections, I wrote the following simple extension method:
public static class RenderPartialCollectionExtension
{
public static void RenderPartialCollection<T>(this HtmlHelper htmlHelper, string partialViewName, IList<T> list)
{
foreach (T item in list)
{
htmlHelper.RenderPartial(partialViewName, item);
}
}
}
This means that instead of the old-style View page code, like this:
<%
foreach (Foo foo in Model)
{
Html.RenderPartial("~/Partials/DisplayFoo.aspx", foo);
}
%>
You can just write code like this:
<% Html.RenderPartialCollection<Foo>("~/Partials/DisplayFoo.aspx", Model); %>