Thoughts from Dan Miser RSS 2.0
# Friday, January 30, 2009

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

Friday, January 30, 2009 4:40:58 PM (Central Standard Time, UTC-06:00)  #    Comments [1] -
ASP.NET MVC
Navigation
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Dan Miser
Sign In
Statistics
Total Posts: 375
This Year: 3
This Month: 0
This Week: 0
Comments: 654
Themes
Pick a theme:
All Content © 2012, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)