<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Tim Scott&#039;s Blog</title>
	<atom:link href="http://lunaverse.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://lunaverse.wordpress.com</link>
	<description>A Blog About .NET Software Development</description>
	<lastBuildDate>Fri, 20 Nov 2009 15:48:21 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='lunaverse.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/31ba1c265ee091facd30874f97c4f2d0?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Tim Scott&#039;s Blog</title>
		<link>http://lunaverse.wordpress.com</link>
	</image>
			<item>
		<title>MVC 2 Areas Gotchas</title>
		<link>http://lunaverse.wordpress.com/2009/11/05/mvc-2-areas-gotchas/</link>
		<comments>http://lunaverse.wordpress.com/2009/11/05/mvc-2-areas-gotchas/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 00:24:23 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[MS MVC]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=836</guid>
		<description><![CDATA[I am adding ASP.NET MVC to a legacy WebForms application.  Hallelujah!  
The process has gone very well.  I am here to report, do not fear the hybrid application!  At first we put the MVC parts directly into the legacy app.  It only required a bit of fiddling around to create a custom view engine. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=836&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I am adding ASP.NET MVC to a legacy WebForms application.  Hallelujah!  </p>
<p>The process has gone very well.  I am here to report, do not fear the hybrid application!  At first we put the MVC parts directly into the legacy app.  It only required a bit of fiddling around to create a custom view engine.  Later we decided to move the MVC parts into an &#8220;area project&#8221; using the new MVC 2.  This <a href="http://msdn.microsoft.com/en-us/library/ee307987(VS.100).aspx">MSDN walkthrough</a> was our guide.  The purpose of this post is to report a couple tricky gotchas that we encountered along the way.</p>
<p><strong>GOTCHA #1</strong>:  By default, the area folder is named after the assembly, not the area name.</p>
<p>The walkthrough shows code for an AreaRegistration class:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Routes : AreaRegistration
{
    <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">string</span> AreaName
    {
        get { <span class="kwrd">return</span> <span class="str">"Store"</span>; }
    }

    <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">void</span> RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            <span class="str">"Store_Default"</span>,
            <span class="str">"Store/{controller}/{action}/{id}"</span>,
            <span class="kwrd">new</span> { controller = <span class="str">"Products"</span>, action = <span class="str">"List"</span>, id = <span class="str">""</span> }
        );
    }
}</pre>
</div>
<p>Notice that the Name property is &#8220;Store&#8221; which matches the first node of the route.  It might seem that this is significant, but as far as I can tell it is not.  What <em>is significant</em> is that the project name &#8220;Store&#8221; matches the first node of the route.  So route needs to start with your assembly name.  </p>
<p>Okay, but what if my porject is named &#8220;Foo.Bar.Store&#8221;?  I don&#8217;t want my route to be &#8220;<strong>Foo.Bar.Store</strong>/{controller}/{action}/{id}&#8221;.  Fortuantely you can change this.  Here&#8217;s how.</p>
<p>The walkthrough gives instructions to modify the project files, which includes adding the following AfterBuild task to your &#8220;area project:&#8221;</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">CreateAreaManifest</span> <span class="attr">AreaName</span>=”$(<span class="attr">AssemblyName</span>)” <span class="attr">AreaType</span>=”<span class="attr">Child</span>” <span class="attr">AreaPath</span>=”$(<span class="attr">ProjectDir</span>)” <span class="attr">ManifestPath</span>=”$(<span class="attr">AreasManifestDir</span>)” <span class="attr">ContentFiles</span>=”@(<span class="attr">Content</span>)” <span class="kwrd">/&gt;</span></pre>
<p>Change it to this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">CreateAreaManifest</span> <span class="attr">AreaName</span>=”<strong>Store</strong>” <span class="attr">AreaType</span>=”<span class="attr">Child</span>” <span class="attr">AreaPath</span>=”$(<span class="attr">ProjectDir</span>)” <span class="attr">ManifestPath</span>=”$(<span class="attr">AreasManifestDir</span>)” <span class="attr">ContentFiles</span>=”@(<span class="attr">Content</span>)” <span class="kwrd">/&gt;</span></pre>
<p>What this does is tells MSBuild to copy the view folders from the &#8220;area project&#8221; into the parent project to a location named &#8220;Views/Areas/Store&#8221; rather than &#8220;Views/Areas/Foo.Bar.Store&#8221;.</p>
<p><strong>GOTCHA #2</strong>: Manifest files are not recreated with every build.</p>
<p>The CreateAreaManifest task does not actually tell MS Build where to copy the view files and folders.  Rather it tells MSBuild how to create a manifest file.  This file contains the details that MSBuild will later use to copy the view files and folders.  But there is a gotcha.  Once a manifest file has been created, subsequent builds add to it, rather than to trash it and create a fresh one.  Therefore, if you change the CreateManifestArea task in the project file, you will need to manually delete the corresponding manifest file.  This is a real pain, and maybe there&#8217;s some way around it, but not that I know about.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/836/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=836&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2009/11/05/mvc-2-areas-gotchas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>
	</item>
		<item>
		<title>The Tale Of The Tape</title>
		<link>http://lunaverse.wordpress.com/2009/09/29/the-tale-of-the-tape/</link>
		<comments>http://lunaverse.wordpress.com/2009/09/29/the-tale-of-the-tape/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 04:44:36 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=782</guid>
		<description><![CDATA[Joel Spolsky recently set off another flurry of discussion in the blogosphere with his post extolling the noble duct tape programmer.  Most of it was not too bad.  But what could have been a nice rant against architecture astronouts was ruined when he drew a false dichotomy between overwrought architecture and duct tape.
Which brings me [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=782&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://www.joelonsoftware.com/">Joel Spolsky</a> recently set off another flurry of discussion in the blogosphere with his post extolling the noble <a href="http://www.joelonsoftware.com/items/2009/09/23.html">duct tape programmer</a>.  Most of it was <a href="http://blog.objectmentor.com/articles/2009/09/24/the-duct-tape-programmer">not too bad</a>.  But what could have been a nice rant against <a href="http://www.joelonsoftware.com/items/2008/05/01.html">architecture astronouts</a> was ruined when he drew a <a href="http://en.wikipedia.org/wiki/False_dilemma">false dichotomy</a> between overwrought architecture and duct tape.</p>
<p>Which brings me to my story.</p>
<p>Just today I came across a charming little piece of duct tape. Here&#8217;s how I imagine it got there.  During construction of the system some tester reported a problem: &#8220;When I do [x] I get an ugly error, and I cannot complete the task.&#8221;  The job of fixing the error was quickly dispatched to a clever duct tape programmer who &#8220;solved&#8221; it equally quickly by <strong>wrapping about 100 lines of code in a try-catch block that swallows the exception</strong>.  Before you groan too loudly I should mention that this code has been in production for a year or more.  Does that prove that duct tape was the right tool?</p>
<p>Not so fast.  Back to what happened today.  A colleague and I spent about an hour scratching our heads over a failing acceptance test.  It turned out that this little piece of tape was masking a problem elsewhere in the code.  What should have taken five minutes to solve took two person hours.</p>
<p>Is it a good trade off?  Absolutely!  Um well, unless the business needs to change.  The thing is, it does need to change.  It needs to change a lot.  I will venture to say that all businesses need to change all the time.  This particular system was built by a team of duct tapers.  So our little struggle today is repeated over and over all day every day.  The net result is that it takes many times longer than it &#8220;should&#8221; to maintain and extend the system.  Indeed, some extenstions seem out of reach.  Conclusion: while duct tape systems may get to v1 faster, they can be very very expensive over the long term in terms of programming labor and worse yet in terms of  lost opportunity.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/782/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/782/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/782/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/782/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/782/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/782/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/782/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/782/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/782/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/782/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=782&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2009/09/29/the-tale-of-the-tape/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>
	</item>
		<item>
		<title>ShouldIt Gets More Fluent</title>
		<link>http://lunaverse.wordpress.com/2009/07/17/740/</link>
		<comments>http://lunaverse.wordpress.com/2009/07/17/740/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 03:26:45 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=740</guid>
		<description><![CDATA[ShouldIt now gives you the option to use a more fluent syntax.  For example, the following:

result.ShouldNotBeNull();

Can now be expressed as:

result.Should().Not.Be.Null();

You can still chain asserts as before.  So this:

personList
    .ShouldCount(1)
    .ShouldContainOne(x =&#62; x.Id == exptected.Id)
    .FirstName.ShouldEqual("Jim")

Can now alternatively be expressed as:

shouldList
    .Should().Count.Exactly(1)
   [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=740&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://code.google.com/p/shouldit/">ShouldIt</a> now gives you the option to use a more fluent syntax.  For example, the following:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">result.ShouldNotBeNull();</pre>
</div>
<p>Can now be expressed as:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">result.Should().Not.Be.Null();</pre>
</div>
<p>You can still chain asserts as before.  So this:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">personList
    .ShouldCount(1)
    .ShouldContainOne(x =&gt; x.Id == exptected.Id)
    .FirstName.ShouldEqual("Jim")</pre>
</div>
<p>Can now alternatively be expressed as:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">shouldList
    .Should().Count.Exactly(1)
    .Should().Contain.One(x =&gt; x.Id == exptected.Id)
    .FirstName.Should().Equal("Jim");</pre>
</div>
<p>The new syntax can be found in the ShouldIt.Clr.Fluent namespace in the Should.Clr assembly.</p>
<p>Why the new syntax?  Whether it&#8217;s more or less readable is largely a matter of taste.  For me, words separated by dots are an improvement over Pascal cased phrases.    However, the extra parentheses surely do not help.  (Anyone else wish that parens were optional for calls to methods with no parameters?)  Nonetheless, I think that the new syntax has some nice advantages.</p>
<ol>
<li><strong>Better Intellisense.</strong> With the old style, Intellisense offers a long list of every possible assertion for the target type.   It&#8217;s annoying to scroll through the list and/or type the entire word &#8220;should&#8221; plus one more letter before the list shirinks.   Using the new style, only one extension method appears at first.   As you type each new node, the list remains very small until you narrow down to the assertion that you want.    Overall I believe that this will facilitate faster and more accurate coding.</li>
<li><strong>More Maintainable And Extensible.</strong> The original extension methods all live together in one big fat static class.  The new code is more object oriented and spreads responsibilities over more small classes.  While this does not immediately provide any value to the user, it will make it easier to add nice features over time.</li>
</ol>
<p>Give it a try and let me know what you think.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/740/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/740/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/740/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/740/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/740/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/740/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/740/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/740/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/740/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/740/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=740&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2009/07/17/740/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>
	</item>
		<item>
		<title>Reusable &#8220;Should&#8221; Extensions</title>
		<link>http://lunaverse.wordpress.com/2009/05/31/rusable-should-extensions/</link>
		<comments>http://lunaverse.wordpress.com/2009/05/31/rusable-should-extensions/#comments</comments>
		<pubDate>Sun, 31 May 2009 04:06:41 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[Test Driven Development]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=545</guid>
		<description><![CDATA[Should Extensions
UPDATE (6/9/09): Get the code here.
Some months ago I started wrapping my test asserts in fluent &#8220;should&#8221; extensions, like this:

public static T ShouldEqual&#60;T&#62;(this T actual, T expected)
{
    Assert.AreEqual(expected, actual);
    return actual;
}
Some of these extensions do a little more than just wrap assertions, for example:

public static T ShouldContainOne&#60;T&#62;(this IEnumerable&#60;T&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=545&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h2>Should Extensions</h2>
<p><strong>UPDATE (6/9/09): Get the code <a href="http://code.google.com/p/shouldit/">here</a>.</strong></p>
<p>Some months ago I started wrapping my test asserts in fluent &#8220;should&#8221; extensions, like this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> T ShouldEqual&lt;T&gt;(<span class="kwrd">this</span> T actual, T expected)
{
    Assert.AreEqual(expected, actual);
    <span class="kwrd">return</span> actual;
}</pre>
<p>Some of these extensions do a little more than just wrap assertions, for example:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> T ShouldContainOne&lt;T&gt;(<span class="kwrd">this</span> IEnumerable&lt;T&gt; e, Func&lt;T, <span class="kwrd">bool</span>&gt; predicate)
{
    var match = e.ShouldContainAtLeastOne(predicate);
    <span class="kwrd">if</span> (match.Count() != 1)
    {
        Assert.Fail(<span class="str">"Expecting 1 matching item.  Found {0}."</span>, match.Count());
    }
    <span class="kwrd">return</span> match.First();
}</pre>
<h2>Reuse Dilemma</h2>
<p>I put these extension methods in a static class in a helper project for re-use across test projects within the application.  Then one day I began work on a new application for a new client.  I love my &#8220;should&#8221; extensions and felt I could not live without them.  Cut-and-paste to the rescue.  Yuck.  Now I am on to another application.  Cut-and-paste again.  Get me off this merry-go-round!</p>
<p>I considered creating a common assembly to share across my projects.  However, as a consultant I prefer not to leave behind binary bits with this client and that client.</p>
<p>Making matters worse, the aforementioned applications do not all use the same testing framework.</p>
<h2>Solution</h2>
<p>So I have started developing a library of reusable &#8220;should&#8221; extensions that I am planning to release as open source – tentatively named &#8220;ShouldIt.&#8221;  It will plug-and-play with all the major unit testing frameworks – with zero configuration using <a href="http://www.codeplex.com/MEF">MEF</a>.  To use it, with NUnit for example, you will reference ShouldIt.Core, ShouldIt.Clr and ShouldIt.NUnit in your test project.</p>
<p>You might be wondering, what is ShouldIt.Clr, and why is it separate from ShouldIt.Core?  ShouldIt.Clr will provide extensions to the CLR base class library types.  This leaves room to add, for example, ShouldIt.Watin.</p>
<p>By the way I considered writing my own assertions instead of wrapping those provided by the testing framework.  Then I opened NUnit and MbUnit in Reflector.  Whew!  There&#8217;s a lot more to those simple-looking assertions than I had expected.</p>
<h2>Challenges And Questions</h2>
<p>The biggest challenge I foresee is managing multiple binary dependencies to the testing frameworks.  This is mitigated by the fact that references will not be version specific.  Hence, pain will only be felt when the Assert APIs, more specifically those methods that ShouldIt calls, are changed.  I expect that those APIs are fairly stable.  That said, we can expect major releases to have breaking changes, as we have seen recently with MbUnit 3 (Gallio).</p>
<p>My questions for you:</p>
<ol>
<li>Anyone else feeling this pain?</li>
<li>Has someone solved this already and I just have not seen it?</li>
<li>Can you think of a better name than &#8220;ShouldIt?&#8221;</li>
</ol>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/545/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/545/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/545/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=545&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2009/05/31/rusable-should-extensions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>
	</item>
		<item>
		<title>Use MvcContrib.FluentHtml With Spark View Engine</title>
		<link>http://lunaverse.wordpress.com/2009/05/18/use-mvccontrib-fluenthtml-with-spark-view-engine/</link>
		<comments>http://lunaverse.wordpress.com/2009/05/18/use-mvccontrib-fluenthtml-with-spark-view-engine/#comments</comments>
		<pubDate>Mon, 18 May 2009 23:04:23 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[FluentHtml]]></category>
		<category><![CDATA[MS MVC]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=482</guid>
		<description><![CDATA[Introduction
MvcContrib.FluentHtml is a library of extensible HTML helpers for MS MVC.  It works out of the box with the WebForms view engine.  As it turns out, it&#8217;s not very hard to use with other view engines.  Read on to see how to use FluentHtml with the Spark view engine.
View Class
First, you need to add [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=482&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><h1>Introduction</h1>
<p><a href="http://www.codeplex.com/MVCContrib">MvcContrib</a>.<a href="http://mvccontrib.codeplex.com/Wiki/View.aspx?title=FluentHtml&amp;referringTitle=Home">FluentHtml</a> is a library of extensible HTML helpers for MS MVC.  It works out of the box with the WebForms view engine.  As it turns out, it&#8217;s not very hard to use with other view engines.  Read on to see how to use FluentHtml with the <a href="http://sparkviewengine.com/">Spark view engine</a>.</p>
<h1>View Class</h1>
<p>First, you need to add a class to your project, something like this:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> SparkModelViewPage&lt;T&gt; : SparkView&lt;T&gt;, IViewModelContainer&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span>
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> List&lt;IBehaviorMarker&gt; behaviors = <span class="kwrd">new</span> List&lt;IBehaviorMarker&gt;();

    <span class="kwrd">protected</span> SparkModelViewPage()
    {
        behaviors.Add(<span class="kwrd">new</span> ValidationBehavior(() =&gt; ViewData.ModelState));
        //add any other desired behaviors here
    }

    <span class="kwrd">public</span> <span class="kwrd">string</span> HtmlNamePrefix { get; set; }

    <span class="kwrd">public</span> T ViewModel
    {
        get { <span class="kwrd">return</span> Model; }
    }

    <span class="kwrd">public</span> IEnumerable&lt;IBehaviorMarker&gt; Behaviors
    {
        get { <span class="kwrd">return</span> behaviors; }
    }
}</pre>
</div>
<p>NOTE: I updated this class since the original post.  I removed constructors overrides that pass in behaviors ad htlmPrefix.  You don&#8217;t need these unless you expect to further derive this class.</p>
<h1>Configuration</h1>
<p>Then apply a little configuration – either in the config file or in code.  Refer to the <a href="http://sparkviewengine.com/documentation/configuring">Spark config documentation</a> for details.</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">var settings = <span class="kwrd">new</span> SparkSettings()
    .SetPageBaseType(<span class="str">"SparkModelViewPage"</span>)
    .AddAssembly(<span class="str">"MvcContrib.FluentHtml"</span>)
    .AddNamespace("MvcContrib.FluentHtml")
    ...yadda yadda;
ViewEngines.Engines.Add(new SparkViewFactory(settings));</pre>
</div>
<p>PageBaseType tells the Spark view engine to use your new class as the base class for all views.  The other two settings tell the Spark view engine to use FluentHtml in views.</p>
<h1>Usage</h1>
<p>That&#8217;s it.  Then you can use FluentHtml in your views like so:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">viewdata</span> <span class="attr">model</span><span class="kwrd">="MyApp.Models.Person"</span><span class="kwrd">/&gt;</span>  

<span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x.FirstName).Label(<span class="str">"First Name"</span>) <span class="asp">%&gt;</span>
<span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x.LastName).Label(<span class="str">"Last Name"</span>) <span class="asp">%&gt;</span></pre>
</div>
<h1>Conclusion</h1>
<p>I should add one caveat.  I have tested only the most naive scenarios, and I have not used Spark for any production work at all.  Please comment on any issues you encounter in using FluentHtml with Spark.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/482/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/482/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/482/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=482&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2009/05/18/use-mvccontrib-fluenthtml-with-spark-view-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>
	</item>
		<item>
		<title>Eliminate Magic Strings In Javascript With MvcContrib.FluentHtml</title>
		<link>http://lunaverse.wordpress.com/2009/02/05/eliminate-magic-strings-in-javascript-with-mvccontribfluenthtml/</link>
		<comments>http://lunaverse.wordpress.com/2009/02/05/eliminate-magic-strings-in-javascript-with-mvccontribfluenthtml/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 03:27:16 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[FluentHtml]]></category>
		<category><![CDATA[MS MVC]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=432</guid>
		<description><![CDATA[MvcContrib.FluentHtml provides a library of HTML helper methods that use expressions with strongly typed views to generate HTML elements.  Not too long after that MS MVC beta  introduced HtmlHelper&#60;T&#62; which allows users to create extension methods that use expressions in the same way.    The benefits are obvious.  By trading magic strings for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=432&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://www.codeplex.com/MVCContrib">MvcContrib</a>.<a href="http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/">FluentHtml</a> provides a library of HTML helper methods that use expressions with strongly typed views to generate HTML elements.  Not too long after that MS MVC beta  introduced HtmlHelper&lt;T&gt; which allows users to create extension methods that use expressions in the same way.    The benefits are obvious.  By trading magic strings for expressions we gain compile time checking, better refactoring, etc.</p>
<p>So this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x.User.FirstName)<span class="asp">%&gt;</span></pre>
<p>Generates this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">type</span><span class="kwrd">="text"</span> <span class="attr">name</span><span class="kwrd">="User.FirstName"</span> <span class="attr">id</span><span class="kwrd">="User_FirstName"</span> <span class="attr">value</span><span class="kwrd">="Joe"</span><span class="kwrd">/&gt;</span></pre>
<p>This is very nice, but what about all the other magic strings in our view?   I&#8217;m talking of course about Javascript.  Does this look familiar?</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">$('#User_FirstName').change(doSomething);</pre>
<p>Doh!  We just can&#8217;t escape those magic strings.  Or can we?  How about this?</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">$('#<span class="asp">&lt;%</span>=<span class="kwrd">this</span>.IdFor(x =&gt; x.User.FirstName)<span class="asp">%&gt;</span>').change(doSomething);</pre>
<p>MsMvc.FluentHtml has introduced IdFor and NameFor. Okay, so we won&#8217;t eliminate <em>all</em> magic string in Javascript but perhaps quite a few. Perhaps we give up a tiny bit of readability, but I think it&#8217;s worth it to stop fretting whether some refactor is going to break a bunch of Javascript.</p>
<p>I owe credit to <a href="http://www.lostechies.com/blogs/jimmy_bogard/default.aspx">Jimmy Bogard</a> for planting this idea in my mind in one of his tweets.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/432/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/432/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/432/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/432/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/432/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/432/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/432/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/432/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/432/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/432/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=432&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2009/02/05/eliminate-magic-strings-in-javascript-with-mvccontribfluenthtml/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>
	</item>
		<item>
		<title>Editing a Variable Length List Of Items With MvcContrib.FluentHtml &#8211; Take 2</title>
		<link>http://lunaverse.wordpress.com/2009/01/13/editing-a-variable-length-list-of-items-with-mvccontribfluenthtml-take-2/</link>
		<comments>http://lunaverse.wordpress.com/2009/01/13/editing-a-variable-length-list-of-items-with-mvccontribfluenthtml-take-2/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 23:48:12 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[FluentHtml]]></category>
		<category><![CDATA[MS MVC]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=347</guid>
		<description><![CDATA[In a previous post, I showed how to edit a variable length list using MvcContrib.FluentHtml.  The post was inspired by a post on the same topic by Steve Sanderson.  Steve commented on my post pointing out some limitations.  To address these limitations required some enhancements to MvcContrib.FluentHtml and some changes to my example.  So here [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=347&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In a <a href="http://lunaverse.wordpress.com/2009/01/02/editing-a-variable-length-list-of-items-with-mvccontribfluenthtml/">previous post</a>, I showed how to edit a variable length list using MvcContrib.FluentHtml.  The post was inspired by a <a href="http://blog.codeville.net/2008/12/22/editing-a-variable-length-list-of-items-in-aspnet-mvc/">post on the same topic by Steve Sanderson</a>.  Steve commented on my post pointing out some limitations.  To address these limitations required some enhancements to MvcContrib.FluentHtml and some changes to my example.  So here we go with take 2.</p>
<h1>MvcContrib.FluentHtml Enhancements</h1>
<p>The limitations were mostly related to handling validation.  To support  validation  using Model State we have added to MvcContrib.FluentHtml:</p>
<ul>
<li>Html Prefix Support</li>
<li>A Validation Helper</li>
<li>List Indexing Support</li>
</ul>
<h2>Html Prefix Support</h2>
<p>HTML elements generated for strongly typed views have no prefix by default.  So elements for ModelViewPage&lt;Person&gt; might look something like this:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">name</span><span class="kwrd">="FirstName"</span> <span class="attr">id</span><span class="kwrd">="FirstName"</span> <span class="attr">value</span><span class="kwrd">="Jim"</span> <span class="attr">type</span><span class="kwrd">="text"</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">name</span><span class="kwrd">="LastName"</span> <span class="attr">id</span><span class="kwrd">="LastName"</span> <span class="attr">value</span><span class="kwrd">="Smith"</span> <span class="attr">type</span><span class="kwrd">="text"</span><span class="kwrd">/&gt;</span></pre>
</div>
<p>The problem is, neither MvcContrib&#8217;s NameValueSerializer nor MS MVC&#8217;s default model binder fully handle this scenario.   While NameValueDeserializer does the binding just fine, it does not place bind errors into ModelState.  The default model binder does not handle quite handle the binding &#8212; it does not deserialize enumerable properties when no prefix is used.</p>
<p>To bind everything correctly <em>and </em>get any errors into ModelState we need a prefix.  One way to handle this is to wrap our primary model in a view model.  This is a valid pattern.  It&#8217;s even a standard for some applications.  However, we don&#8217;t want to <em>have to</em> do this just to get a prefix.  So we provide a way to specify a prefix for a strongly typed view.</p>
<p>Either in the code-behind:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Index : ModelViewPage&lt;IList&lt;Gift&gt;&gt;
{
    <span class="kwrd">public</span> Index() : <span class="kwrd">base</span>(<span class="str">"person"</span>) { }
}</pre>
</div>
<p>Or in the view:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">&lt;%<span class="kwrd">this</span>.HtmlNamePrefix = <span class="str">"person"</span>%&gt;</pre>
</div>
<p>Thus our HTML elements will be prefixed:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">name</span><span class="kwrd">="person.FirstName"</span> <span class="attr">id</span><span class="kwrd">="person_FirstName"</span> <span class="attr">value</span><span class="kwrd">="Jim"</span> <span class="attr">type</span><span class="kwrd">="text"</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">name</span><span class="kwrd">="person.LastName"</span> <span class="attr">id</span><span class="kwrd">="person_LastName"</span> <span class="attr">value</span><span class="kwrd">="Smith"</span> <span class="attr">type</span><span class="kwrd">="text"</span><span class="kwrd">/&gt;</span></pre>
</div>
<p>Then in our action method, we can use the default binder without any attribute.  Any bind errors will be captured in ModelState.</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">[AcceptPost]
<span class="kwrd">public</span> ViewResult Index(Person person)</pre>
</div>
<h2>New Validation Helper</h2>
<p>We have added validation support to FluentHtml for strongly typed views.  The following works basically the same as HtmlHelper&#8217;s ValidationMessage.</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="asp">&lt;%</span><span class="kwrd">this</span>.ValidationMessage(x =&gt; x.Price, <span class="str">"Price must be numeric."</span>)<span class="asp">%&gt;</span></pre>
</div>
<p>We have also added a behavior to ModelViewPage&lt;T&gt;, ModelViewMasterPage&lt;T&gt; and ModelViewUserControl&lt;T&gt; which basically mimics validation used by HtmlHelper.  That is, it adds a CSS class &#8220;input-validation-error&#8221; to any HTML element with an error in ModelState.  If you wish, you can remove this behavior or change the CSS class name in the derived class.</p>
<h2>List Indexing Support</h2>
<p>MS MVC uses <a href="http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx">a special technique</a> to deserialize enumerable properties.  With this technique you set an arbitrary value in a specially named hidden element to signify that a group of elements belongs to a particular instance of the enumerable property.  This has an advantage over using positional indexes in that it preserves the identity of an instance across posts, which is necessary for ModelState based validation to work properly.</p>
<p>Therefore, we have added support for this to MvcContrib.FluentHtml.  Let&#8217;s assume ModelViewPage&lt;IList&lt;Person&gt;&gt; with a prefix of &#8220;persons&#8221;.  Then this markup:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="asp">&lt;%</span>var id = Model[i].Id;<span class="asp">%&gt;</span>
<span class="asp">&lt;%</span>=<span class="kwrd">this</span>.Index(x =&gt; x).Value(id)<span class="asp">%&gt;</span>
<span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x[id].FirstName).Value(Model[i].FirstName)<span class="asp">%&gt;</span></pre>
</div>
<p>Will generate HTML like this:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">id</span><span class="kwrd">="persons_Index_123"</span> <span class="attr">name</span><span class="kwrd">="persons.Index"</span> <span class="attr">value</span><span class="kwrd">="123"</span> <span class="attr">type</span><span class="kwrd">="hidden"</span><span class="kwrd">/&gt;</span>
<span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">id</span><span class="kwrd">="persons[123]_FirstName"</span> <span class="attr">name</span><span class="kwrd">="persons[123].FirstName"</span> <span class="attr">value</span><span class="kwrd">="Jim" type="text"/</span><span class="kwrd">&gt;
</span></pre>
</div>
<h1>Changes To Our Example</h1>
<p>So based on these changes to FluentHtml we made a few changes to the &#8220;Gift Request Form&#8221; demo from the previous post.  In the main view:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="asp">&lt;%</span><span class="kwrd">for</span> (var i = 0; i &lt; ViewData.Model.Count; i++) {<span class="asp">%&gt;</span>
    <span class="asp">&lt;%</span>ViewData[<span class="str">"index"</span>] = i;
    Html.RenderPartial(<span class="str">"GiftLineItem"</span>);<span class="asp">%&gt;</span>
<span class="asp">&lt;%</span>}<span class="asp">%&gt;</span></pre>
</div>
<p>And in the user control:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="asp">&lt;%</span>var i = (<span class="kwrd">int</span>)ViewData[<span class="str">"index"</span>];
  var gift = ViewModel == <span class="kwrd">null</span> ? <span class="kwrd">null</span> : ViewModel[i];
  var id = gift == <span class="kwrd">null</span> ? -1 * <span class="kwrd">new</span> Random().Next() : gift.Id;
  var name = gift == <span class="kwrd">null</span> ? <span class="kwrd">null</span> : gift.Name;
  var price = gift == <span class="kwrd">null</span> ? (<span class="kwrd">decimal</span>?)<span class="kwrd">null</span> : gift.Price;<span class="asp">%&gt;</span>
<span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">class</span><span class="kwrd">="giftLineItem"</span><span class="kwrd">&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.Index(x =&gt; x, x =&gt; x[i].Id)<span class="asp">%&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.Hidden(x =&gt; x[id].Id).Value(id)<span class="asp">%&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x[id].Name).Value(name).Label(<span class="str">"Name of gift:"</span>)<span class="asp">%&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x[id].Price).Value(price).Label(<span class="str">"Price ($):"</span>)<span class="asp">%&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.ValidationMessage(x =&gt; x[id].Price, <span class="str">"Must be a number"</span>)<span class="asp">%&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">a</span> <span class="attr">href</span><span class="kwrd">=""</span> <span class="attr">class</span><span class="kwrd">="removeGift"</span><span class="kwrd">&gt;</span>Delete<span class="kwrd">&lt;/</span><span class="html">a</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>
</div>
<p>Viola, validation works:</p>
<p><img class="alignnone size-full wp-image-381" title="showvalidationerror1" src="http://lunaverse.files.wordpress.com/2009/01/showvalidationerror1.png?w=600&#038;h=201" alt="showvalidationerror1" width="600" height="201" /></p>
<p>Note that we stole Steve&#8217;s technique of using random negative Ids for unsaved instances, which we will handle in the save method on the server.</p>
<p><a href="http://www.lunaversesoftware.com/codebits/MvcEditableListDemo2.zip">GET THE REVISED CODE</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/347/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/347/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/347/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=347&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2009/01/13/editing-a-variable-length-list-of-items-with-mvccontribfluenthtml-take-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>

		<media:content url="http://lunaverse.files.wordpress.com/2009/01/showvalidationerror1.png" medium="image">
			<media:title type="html">showvalidationerror1</media:title>
		</media:content>
	</item>
		<item>
		<title>Editing a Variable Length List Of Items With MvcContrib.FluentHtml</title>
		<link>http://lunaverse.wordpress.com/2009/01/02/editing-a-variable-length-list-of-items-with-mvccontribfluenthtml/</link>
		<comments>http://lunaverse.wordpress.com/2009/01/02/editing-a-variable-length-list-of-items-with-mvccontribfluenthtml/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 21:28:32 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[FluentHtml]]></category>
		<category><![CDATA[MS MVC]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=164</guid>
		<description><![CDATA[Steve Sanderson recently showed  a nifty way to edit variable length lists with MS MVC .  I commented that I liked his approach.  I also mentioned that MvcContrib.FluentHtml might be used to improve a bit of awkward code used to build HTML element names.  To illustrate my point I offered some code examples.   Steve responded [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=164&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Steve Sanderson recently showed  <a href="http://blog.codeville.net/2008/12/22/editing-a-variable-length-list-of-items-in-aspnet-mvc/">a nifty way to edit variable length lists with MS MVC</a> .  I commented that I liked his approach.  I also mentioned that MvcContrib.FluentHtml might be used to improve a bit of awkward code used to build HTML element names.  To illustrate my point I offered some code examples.   Steve responded by pointing out the shortcomings of  my examples.  He was correct.   I had not really thought it through.  While my examples illustrated the use of FluentHtml, they missed the mark in terms of showing a real solution.</p>
<p>Still I knew that the basic idea of was sound.  This article shows in detail how to use MvcContrib.FluentHtml to edit a variable length list of items.</p>
<p><a href="http://www.lunaversesoftware.com/codebits/MvcEditableListDemo.zip">Get The Code</a></p>
<p>I will use the same basic application as Steve did &#8212; a simple gift request form.</p>
<h2>Setup</h2>
<p>We start with the Gift entity:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Gift
{
    <span class="kwrd">public</span> <span class="kwrd">int</span> Id { get; set; }
    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }
    <span class="kwrd">public</span> <span class="kwrd">decimal</span> Price { get; set; }
}</pre>
</div>
<h2>Render The View</h2>
<p>Our view derives from MvcContrib.FluentHtml.ModelViewPage&lt;IList&lt;Gift&gt;&gt; and is rendered via a controller action:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">[AcceptGet]
<span class="kwrd">public</span> ViewResult Index()
{
     ViewData.Model = <span class="kwrd">new</span> List&lt;Gift&gt;
     {
         <span class="kwrd">new</span> Gift {Id = 1, Name = <span class="str">"Tar Tinker"</span>, Price = 23.44m},
         <span class="kwrd">new</span> Gift {Id = 2, Name = <span class="str">"Flu Flooper"</span>, Price = 11.42m}
     };
     <span class="kwrd">return</span> View();
}</pre>
</div>
<p>The view presents a list of the gifts:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">form</span> <span class="attr">action</span>="&lt;%=<span class="attr">Html</span>.<span class="attr">BuildUrlFromExpression&lt;</span><span class="attr">HomeController</span><span class="kwrd">&gt;</span>(x =<span class="kwrd">&gt;</span> x.Index(null))<span class="asp">%&gt;</span>" method="post"<span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">id</span><span class="kwrd">="addGiftItem"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">a</span> <span class="attr">href</span><span class="kwrd">=""</span> <span class="attr">id</span><span class="kwrd">="addGift"</span><span class="kwrd">&gt;</span>Add<span class="kwrd">&lt;/</span><span class="html">a</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">fieldset</span> <span class="attr">id</span><span class="kwrd">="giftLineItems"</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">legend</span><span class="kwrd">&gt;</span>Gifts<span class="kwrd">&lt;/</span><span class="html">legend</span><span class="kwrd">&gt;</span>
        <span class="asp">&lt;%</span><span class="kwrd">for</span> (var i = 0; i &lt; ViewData.Model.Count; i++) {<span class="asp">%&gt;</span>
            <span class="asp">&lt;%</span>Html.RenderPartial(<span class="str">"GiftLineItem"</span>, ViewData.Model, <span class="kwrd">new</span> ViewDataDictionary {{<span class="str">"index"</span>, i}});<span class="asp">%&gt;</span>
        <span class="asp">&lt;%</span>}<span class="asp">%&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">fieldset</span><span class="kwrd">&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.SubmitButton(<span class="str">"Save"</span>)<span class="asp">%&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">form</span><span class="kwrd">&gt;</span></pre>
</div>
<p>GiftLineItem is a user control that derives from MvcContrib.FluentHtml.ViewModelUserControl&lt;IList&lt;Gift&gt;&gt;:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="asp">&lt;%</span>var i = (<span class="kwrd">int</span>)ViewData[<span class="str">"index"</span>];<span class="asp">%&gt;</span>
<span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">class</span><span class="kwrd">="giftLineItem"</span><span class="kwrd">&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.Hidden(x =&gt; x[i].Id)<span class="asp">%&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x[i].Name).Label(<span class="str">"Name of gift:"</span>)<span class="asp">%&gt;</span>
    <span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x[i].Price).Label(<span class="str">"Price ($):"</span>)<span class="asp">%&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">a</span> <span class="attr">href</span><span class="kwrd">=""</span> <span class="attr">class</span><span class="kwrd">="removeGift"</span><span class="kwrd">&gt;</span>Delete<span class="kwrd">&lt;/</span><span class="html">a</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>
</div>
<p>You might observe that this user control uses IList&lt;Gift&gt; as its model whereas Steve&#8217;s uses Gift.    But the user control represents one gift, right?  Have I sacrificed cohesion?  Yes, however a closer look shows that neither Steve&#8217;s user control nor mine is cohesive.  That is, neither stands alone outside of the context of a list.  We simply use different techniques to name the elements within the context of an owning list.</p>
<p>Here&#8217;s the rendered view (after clicking &#8220;Add&#8221; twice):</p>
<p><img class="alignnone size-full wp-image-230" title="variablelineitemdemo1.gif" src="http://lunaverse.files.wordpress.com/2009/01/variablelineitemdemo1.gif?w=600&#038;h=306" alt="variablelineitemdemo1.gif" width="600" height="306" /></p>
<h2>Add Behavior</h2>
<p>Now we need to add behavior to the &#8220;Add&#8221; and &#8220;Delete&#8221; links.  For that we&#8217;ll use jQuery:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">$(document).ready(function() {

    var nextGiftIndex = <span class="asp">&lt;%</span>=ViewData.Model.Count<span class="asp">%&gt;</span>;
    setRemoveLinks();

    $('#addGift').click(function() {
        $.get('/Home/AddGift?index=' + nextGiftIndex, 'html', function(lineItemHtml) {
            $('#giftLineItems').append(lineItemHtml);
            setRemoveLinks();
        });
        nextGiftIndex++;
        return false;
    });
});
function setRemoveLinks() {
    $('a.removeGift').click(function() {
        $(this).parent('div').remove();
        return false;
    });
}</pre>
</div>
<p>Clicking the &#8220;Add&#8221; link calls the AddGift action asyncronously and passes the index of the item to be added.  The index value is initialized when the page is rendered and incremented on the client side with each addition.  The &#8220;Delete&#8221; links are wired up to a simple function that removes the parent div.</p>
<p>The AddGift action simply renders the user control using the specified index:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">public</span> ViewResult AddGift(<span class="kwrd">int</span> index)
{
    ViewData[<span class="str">"index"</span>] = index;
    <span class="kwrd">return</span> View(<span class="str">"~/Views/Home/GiftLineItem.ascx"</span>);
}</pre>
</div>
<p>Notice we have not specified a model for the user control, which means it will be null.  As a result, the name and price textboxes will be blank, which is what we want.  And by specifying the desired index, these textboxes will be named such that they will bind correctly when form is saved.</p>
<h2>Save Changes</h2>
<p>Clicking the &#8220;Save&#8221; button posts the form to the following action:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">[AcceptPost]
<span class="kwrd">public</span> ViewResult Index([Deserialize]IList&lt;Gift&gt; gifts)
{
    <span class="rem">//code to save the changes</span>
    ViewData.Model = gifts;
    <span class="kwrd">return</span> View();
}</pre>
</div>
<p>Notice that the parameter &#8220;gifts&#8221; is decorated with the Deserialize attribute.  This attribute uses MvcContrib&#8217;s NameValueDeserializer to obtain our collection of gifts from the form post.  It does not require that collections be indexed purely in sequence.  Thus it handles any holes left by deleted line items.</p>
<p>(NOTE: I expected that using [Bind(Prefix = "")] would work the same as [Deserialize], but it does not.  Seems to be a limitation.)</p>
<h2>Conclusion</h2>
<p>The solution presented here differs from Steve&#8217;s in that it uses FluentHtml.  There are a few other differences, such as jQuery versus MS Ajax.  My point is not present a solution that is superior, rather just to show how it might be done using  FluentHtml and expressions to avoid some fiddly construction of HTML element names in views.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/164/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=164&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2009/01/02/editing-a-variable-length-list-of-items-with-mvccontribfluenthtml/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>

		<media:content url="http://lunaverse.files.wordpress.com/2009/01/variablelineitemdemo1.gif" medium="image">
			<media:title type="html">variablelineitemdemo1.gif</media:title>
		</media:content>
	</item>
		<item>
		<title>MvcFluentHtml Moved To MvcContrib</title>
		<link>http://lunaverse.wordpress.com/2008/12/13/mvcfluenthtml-moved-to-mvccontrib/</link>
		<comments>http://lunaverse.wordpress.com/2008/12/13/mvcfluenthtml-moved-to-mvccontrib/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 00:12:31 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[MS MVC]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=158</guid>
		<description><![CDATA[MvcFluentHtml, discussed in this blog post, has been moved to MvcContrib.
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=158&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>MvcFluentHtml, discussed in <a href="http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/">this blog post</a>, has been moved to <a href="http://www.codeplex.com/MVCContrib">MvcContrib</a>.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=158&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2008/12/13/mvcfluenthtml-moved-to-mvccontrib/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>
	</item>
		<item>
		<title>MvcFluentHtml &#8211; Fluent HTML Interface For MS MVC</title>
		<link>http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/</link>
		<comments>http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 18:45:19 +0000</pubDate>
		<dc:creator>Tim Scott</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[FluentHtml]]></category>
		<category><![CDATA[MS MVC]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://lunaverse.wordpress.com/?p=24</guid>
		<description><![CDATA[UPDATE: MvcFluentHtml has been moved to MvcContrib where it lives in its own assembly, MvcContrib.FluentHtml.
A few weeks ago I attended a presentation at the KaizenConf by Jeremy Miller and Chad Myers in which they showed their &#8220;opinionated&#8221; approach to MS MVC.  One of the things that caught my attention was the fluent interface they had [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=24&subd=lunaverse&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><strong>UPDATE: MvcFluentHtml has been moved to <a href="http://www.codeplex.com/MVCContrib">MvcContrib</a> where it lives in its own assembly, MvcContrib.FluentHtml.</strong></p>
<p>A few weeks ago I attended a presentation at the <a href="http://www.kaizenconf.com/">KaizenConf</a> by <a href="http://codebetter.com/blogs/jeremy.miller">Jeremy Miller </a>and <a href="http://www.lostechies.com/blogs/chad_myers">Chad Myers</a> in which they showed their &#8220;opinionated&#8221; approach to MS MVC.  One of the things that caught my attention was the fluent interface they had created for HTML generation.   So I went home and started working on this for my own application.</p>
<p><del>I have put my work in progress here: <a href="http://code.google.com/p/mvcfluenthtml/">MvcFluentHtml</a>.</del></p>
<p>Incidentally, since I started working on MvcFluentHtml, Chad and Jeremy have put out some bits of their code as <a href="http://code.google.com/p/opinionatedmvc/">opinionatedmvc</a>.  And Karl Seguin, who also saw their presentation, has created some fluent HTML stuff which he shows in <a href="http://codebetter.com/blogs/karlseguin/archive/2008/11/13/textboxfor-u-gt-u-name-unleash-the-power.aspx">this blog post</a>.</p>
<p>So what problem does MvcFluentHtml solve?  Can&#8217;t I be happy with the HtmlHelpers that come with the framework?</p>
<p>I have come to truly hate the overloading approach taken by the out-of-the-box Html helpers.  Methods with long signatures are hard to read, and it takes investigation to see what&#8217;s happening.  What&#8217;s worse, you must worry about problems with overload resolution, especially when some parameters are typed as object.  As a result, HtmlHelper is not easily extensible.  It&#8217;s hard to bend it to do new things without breaking existing functionality.  We saw an example of this when Beta1 was released with <a href="http://stackoverflow.com/questions/208468/issues-during-aspnet-mvc-upgrade-from-preview-5-to-beta#219181">breaking changes</a>.  With a fluent interface, it&#8217;s much easier to extend with new behaviors while leaving existing behaviors closed.</p>
<p>So how does it work?  Let&#8217;s start with some examples of how you might use it in your views:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ -->
<p>&nbsp;</p>
<pre class="csharpcode"><span class="asp">&lt;%</span>=<span class="kwrd">this</span>.TextBox(x =&gt; x.FirstName).Class(<span class="str">"required"</span>).Label(<span class="str">"First Name:"</span>)<span class="asp">%&gt;</span>
<span class="asp">&lt;%</span>=<span class="kwrd">this</span>.Select(x =&gt; x.ClientId).Options((SelectList)ViewData[<span class="str">"clients"</span>]).Label(<span class="str">"Client:"</span>)<span class="asp">%&gt;</span>
<span class="asp">&lt;%</span>=<span class="kwrd">this</span>.MultiSelect(x =&gt; x.UserId).Options(ViewModel.Users)%&gt;
<span class="asp">&lt;%</span>=<span class="kwrd">this</span>.CheckBox(<span class="str">"enabled"</span>).LabelAfter(<span class="str">"Enabled"</span>).Title("Click to enable.").Styles(vertical_align =&gt; "middle")<span class="asp">%&gt;</span></pre>
</div>
<p>As you can see in this example, the helpers are methods of the view itself, not of some helper class.  You will also notice two different kinds of methods.  One kind takes the name as a string which is meant to evaluate to an object in the ViewData dictionary.  These methods are extensions of IViewDataContainer, and as such they can be used in any type of view (ViewPage, ViewPage&lt;T&gt;, ViewUserControl, ViewUserControl&lt;T&gt;, ViewMasterPage, ViewMasterPage&lt;T&gt;).   The other kind takes a lambda expression that refers to a member of ViewData.Model.  This kind of method extends a new interface that is defined in <del>MvcFluentHtml</del> MvcContrib.FluentHtml:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ -->
<p>&nbsp;</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IViewModelContainer&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span>
{
    T ViewModel { get; }
    IEnumerable&lt;IMemberBehavior&gt; MemberBehaviors { get; }
}</pre>
</div>
<p>Both kinds of methods set the value (value attribute, inner text, etc.) from ViewData.  Both kinds set the name of form elements so that most binders will pick up the value on post.</p>
<p>So what must you do before you can use these methods in your views?  To use the methods that extend IViewDataContainer, nothing.  To use the methods that extend IViewModelContainer&lt;T&gt;, you have some options:</p>
<ol>
<li>Derive your strongly-typed views from ModelViewPage&lt;T&gt;, ModelViewUserControl&lt;T&gt; or ModelViewMasterPage&lt;T&gt; which are bits of MvcFluentHtml.</li>
<li>Implement IViewModelContainer&lt;T&gt; directly on your views.</li>
<li>Define your own base view classes that implement IViewModelContainer&lt;T&gt;.</li>
</ol>
<p>There is a fourth option.  MvcFluentHtml contains a set of classes, ConventionModelViewPage&lt;T&gt;, ConventionModelViewUserControl&lt;T&gt; and ConventionModelViewMasterPage&lt;T&gt;.  These classes apply some default behaviors.  These behaviors operate on certain model attributes, which are also part of <del>MvcFluentHtml</del> MvcContrib.</p>
<p>So given this model:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ -->
<p>&nbsp;</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Person
{
    [Required]
    [MaxLength(200)]
    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }
}</pre>
</div>
<p>And this in the view:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ -->
<p>&nbsp;</p>
<pre class="csharpcode"><span class="asp">&lt;%</span><span class="kwrd">this</span>.TextBox(x =&gt; x.Name)<span class="asp">%&gt;</span></pre>
</div>
<p>The resulting HTML would be something like:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ -->
<p>&nbsp;</p>
<pre class="csharpcode"><span class="kwrd">&lt;</span><span class="html">input</span> <span class="attr">type</span><span class="kwrd">="text"</span> <span class="attr">maxlength</span><span class="kwrd">="200"</span> <span class="attr">id</span><span class="kwrd">="Name"</span> <span class="attr">name</span><span class="kwrd">="Name"</span> <span class="attr">class</span><span class="kwrd">="required"</span> <span class="attr">value</span><span class="kwrd">="Pete"</span><span class="kwrd">/&gt;</span></pre>
</div>
<p>These attributes are found in a separate assembly, <del>MvcFluentHtml.Attributes</del> MvcContrib.ModelAttributes.  So in case your models are in a separate assembly from your MVC web UI, you don&#8217;t have to reference the UI specific bits in your model assembly.</p>
<p>But what if you want different behaviors and different attributes?  You can create custom behaviors by implementing IMemberBehavior.</p>
<div class="csharpcode">
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IMemberBehavior
{
     <span class="kwrd">void</span> Execute(IMemberElement element);
}</pre>
</div>
<p>For example:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ -->
<p>&nbsp;</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyMemberBehavior1 : IMemberBehavior
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Execute(IMemberElement element)
    {
        var helper = <span class="kwrd">new</span> MemberBehaviorHelper&lt;RequiredAttribute&gt;();
        var attribute = helper.GetAttribute(element);
        <span class="kwrd">if</span> (attribute  != <span class="kwrd">null</span>)
        {
            element.SetAttr(<span class="str">"class"</span>, <span class="str">"req"</span>);
        }
    }
}</pre>
</div>
<p>Then create your own &#8220;opinionated&#8221; views something like this:</p>
<div class="csharpcode"><!-- code formatted by http://manoli.net/csharpformat/ -->
<p>&nbsp;</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyOpinionatedModelViewPage&lt;T&gt; : ModelViewPage&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span>
{
     <span class="kwrd">public</span> MyOpinionatedModelViewPage() : <span class="kwrd">base</span>(<span class="kwrd">new</span> MyMemberBehavior1(), <span class="kwrd">new</span> MyMemberBehavior2()) { }
}</pre>
</div>
<p>Or using your favorite IoC container:</p>
<div class="csharpcode">
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> MyOpinionatedModelViewPage&lt;T&gt; : ModelViewPage&lt;T&gt; <span class="kwrd">where</span> T : <span class="kwrd">class</span>
{
     <span style="text-decoration:line-through;"><span class="kwrd">public</span> MyOpinionatedModelViewPage(IMyMemberBehavior1Marker myBehavior1, IMyMemberBehavior2Marker myBehavior1)
        : <span class="kwrd">base</span>(myMemberBehavior1, myMemberBehavior1) { }</span>
     <span class="kwrd">public</span> MyOpinionatedModelViewPage() : base(IoC.Resolve&lt;IMyMemberBehavior1Marker&gt;(), IoC.Resolve&lt;IMyMemberBehavior2Marker&gt;()) { }

}</pre>
</div>
<p>One final point.  The goal of MvcFluentHtml was to leave the opinions to you.  We did this by allowing you to define your own behaviors.  However, it is not without opinions regarding practices.  For example the Select object does not have any &#8220;first option&#8221; functionality.  That&#8217;s because in my opinion adding options to selects is not a view concern.  Also, while we allow you to set inline styles we do not expose any methods that abstract styling (e.g., Width, TextAlign).  That&#8217;s because we want to encourage semantic HTML and leave the styling to CSS.  For the same reason we do not render any layout tags (e.g. &lt;br/&gt;).  Also we generally don&#8217;t try to abstract the language of HTML (e.g., we use &#8220;Select&#8221; instead of &#8220;DropDownList&#8221;).  In my opinion, MvcFluentHtml is more of a helper library than an abstraction over HTML, and moving developers away from understanding what&#8217;s going on with the HTML is not a good thing.</p>
<p>I heartily welcome critique, especially if you want to contribute!  Areas that I know need some work are: radio button, radio button list, checkbox list, performance tweaks, XML comments, and CI.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/lunaverse.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/lunaverse.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/lunaverse.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/lunaverse.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/lunaverse.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/lunaverse.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/lunaverse.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/lunaverse.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/lunaverse.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/lunaverse.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=lunaverse.wordpress.com&blog=1049806&post=24&subd=lunaverse&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/7bb15b284510700d32de5a68f190c6dc?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tim</media:title>
		</media:content>
	</item>
	</channel>
</rss>