Framework Madness!

And other adventures C# and asp.net …

Asp.net MVC 2 Preview 1 – ‘DisplayItemFor’ custom HtmlHelper extension

with one comment

Yesterday, well really earlier today, I went on an on about the problems with DisplayFor and my initial hack. Then I updated the post highlighting a post from Matt Hidinger that covers an expression-based  alternative. Here is the link:

http://blog2.matthidinger.com/archive/2009/08/15/creating-a-html.displayformany-helper-for-mvc-2.aspx

But then sleep called and then to work and I let the idea brew. I really like Matt’s use of expressions but I didn’t like the fact that I could not control write flow. So I took his code and morphed it into what I’ll call ‘DisplayItemFor’. It’s quite simple: it requires and enumerable expression from the model and item from the same enumerable. At that point setting up a foreach loop and calling ‘DisplayItemFor’ is a fairly straight forward. Here’s an implementation example:

   1: <ul>

   2:     <%
   1: foreach (ArticleElement element in Model.Elements){

%>

   3:         <li>

   4:             <%
   1: =Html.DisplayItemFor(m => m.Elements,element)  

%>

   5:         </li>        

   6:     <%
   1: } 

%>

   7: </ul>

Download the source:

(using .docx due to wordpress limitations)

Advertisements

Written by Lynn Eriksen

September 14, 2009 at 7:39 pm

Posted in Uncategorized

One Response

Subscribe to comments with RSS.

  1. […] – that sucks. And so I went and made workarounds such as DisplayItemFor, and yet found it wanting. And I explored into the bowels of lambda expressions, and I wanted […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: