<?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/"
	>

<channel>
	<title>Technical Jargon &#187; mvccontrib</title>
	<atom:link href="http://www.jeremyskinner.co.uk/category/mvccontrib/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jeremyskinner.co.uk</link>
	<description>Did you notice the information bar?</description>
	<lastBuildDate>Mon, 08 Mar 2010 20:17:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Contributing to MvcContrib using Mercurial</title>
		<link>http://www.jeremyskinner.co.uk/2010/03/08/contributing-to-mvccontrib-using-mercurial/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/03/08/contributing-to-mvccontrib-using-mercurial/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 20:17:49 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[mercurial]]></category>
		<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=485</guid>
		<description><![CDATA[
Now that the source code for MvcContrib is moving back to CodePlex I&#8217;ve written a guide to contributing to MvcContrib using Mercurial.
]]></description>
			<content:encoded><![CDATA[<p>
Now that the source code for <a href="http://mvccontrib.org">MvcContrib</a> is <a href="http://groups.google.com/group/mvccontrib-discuss/browse_thread/thread/8eb52bd3a9c4354c">moving back to CodePlex</a> I&#8217;ve written <a href="http://mvccontrib.codeplex.com/wikipage?title=HowToContribute">a guide to contributing to MvcContrib using Mercurial.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/03/08/contributing-to-mvccontrib-using-mercurial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DevEvening MvcContrib Presentation</title>
		<link>http://www.jeremyskinner.co.uk/2009/10/23/devevening-mvccontrib-presentation/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/10/23/devevening-mvccontrib-presentation/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 16:51:04 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=232</guid>
		<description><![CDATA[
Yesterday I did a presentation at DevEvening on using MvcContrib with ASP.NET MVC applications.


I demonstrated the use of FluentHtml (with behaviours), the Grid and Pager components, the ActionResult assertions and the route testing extensions.


Slides and code can be downloaded here.
]]></description>
			<content:encoded><![CDATA[<p>
Yesterday I did a presentation at <a href="http://www.devevening.co.uk/">DevEvening</a> on using <a href="http://mvccontrib.org">MvcContrib</a> with ASP.NET MVC applications.
</p>
<p>
I demonstrated the use of FluentHtml (with behaviours), the Grid and Pager components, the ActionResult assertions and the route testing extensions.
</p>
<p>
Slides and code can be <a href="http://www.jeremyskinner.co.uk/files/MvcContribPresentation.zip">downloaded here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/10/23/devevening-mvccontrib-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MvcContrib now on GitHub</title>
		<link>http://www.jeremyskinner.co.uk/2009/08/22/mvccontrib-now-on-github/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/08/22/mvccontrib-now-on-github/#comments</comments>
		<pubDate>Sat, 22 Aug 2009 20:03:37 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=185</guid>
		<description><![CDATA[
The MvcContrib source code has recently been moved to GitHub and can now be found at http://github.com/mvccontrib/mvccontrib. There&#8217;s also a branch that builds against MVC 2 Preview 1 at http://github.com/mvccontrib/mvccontrib/tree/mvc2


I&#8217;ve also put together a guide to contributing to MvcContrib using git for those without git experience.
]]></description>
			<content:encoded><![CDATA[<p>
The <a href="http://mvccontrib.org">MvcContrib</a> source code has recently been moved to GitHub and can now be found at <a href="http://github.com/mvccontrib/mvccontrib">http://github.com/mvccontrib/mvccontrib</a>. There&#8217;s also a branch that builds against MVC 2 Preview 1 at <a href="http://github.com/mvccontrib/mvccontrib/tree/mvc2">http://github.com/mvccontrib/mvccontrib/tree/mvc2</a>
</p>
<p>
I&#8217;ve also put together a <a href="http://mvccontrib.github.com/MvcContrib/">guide to contributing to MvcContrib using git</a> for those without git experience.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/08/22/mvccontrib-now-on-github/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MvcContrib Grid Part 5 &#8211; The Action Syntax</title>
		<link>http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 13:33:45 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=121</guid>
		<description><![CDATA[This is part 5 of a series about the new MvcContrib grid.

Part 1 &#8211; Introduction
Part 2 &#8211; New Syntax
Part 3 &#8211; GridModels and GridRenderes
Part 4 &#8211; Limitations of the WebFormsViewEngine

Part 5 &#8211; The Action Syntax



The rewritten MvcContrib grid now has an alternative syntax thanks to the work of Will Shaver. This syntax is called the [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 5 of a series about the new MvcContrib grid.</p>
<ul>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/">Part 2 &#8211; New Syntax</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers">Part 3 &#8211; GridModels and GridRenderes</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/28/mvccontrib-grid-part-4-limitations-of-the-webformsviewengine">Part 4 &#8211; Limitations of the WebFormsViewEngine</a></li>
<li>
<a href="http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax">Part 5 &#8211; The Action Syntax</a>
</li>
</ul>
<p>
The rewritten MvcContrib grid now has an alternative syntax thanks to the work of <a href="http://www.primedigit.com/">Will Shaver</a>. This syntax is called the &#8216;Action&#8217; syntax as it makes heavy use of the System.Action delegate.
</p>
<h2>Enabling the ActionSyntax</h2>
<p>
By default, the methods associated with the Action Syntax will not show up as they are extension methods that live in a different namespace to the rest of the grid. Out of the box only the default syntax will work. To enable the Action Syntax you&#8217;ll need to either import the <strong>MvcContrib.UI.Grid.ActionSyntax</strong> namespace either in your .aspx view&#8230;
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> Import Namespace<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;MvcContrib.UI.Grid.ActionSyntax&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>&#8230;or in your web.config&#8217;s namespace import section:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pages<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;namespaces<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;add</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;MvcContrib.UI.Grid.ActionSyntax&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/namespaces<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pages<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>
Comparing the two syntaxes<br />
</h2>
<p>
There are several differences between the two syntaxes. Here is a grid definition using the default syntax:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model<span style="color: #006600; font-weight:bold;">&#41;</span>
	.<span style="color: #9900cc;">Columns</span><span style="color: #006600; font-weight:bold;">&#40;</span>column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
  		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Named</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Person ID&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Gender</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>    
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Format</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;{0:d}&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;View Person&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Named</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Partial</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;ViewPersonPartial&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span> <span style="color: #ff6600;">//Example of using a Partial view for complex cells</span>
     	<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">RowStart</span><span style="color: #006600; font-weight:bold;">&#40;</span>row <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #990099; font-weight: bold;">string</span>.<span style="color: #9900cc;">Format</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&lt;tr{0}&gt;&quot;</span>, row.<span style="color: #9900cc;">IsAlternate</span> <span style="color: #006600; font-weight: bold;">?</span> <span style="color: #cc0000;">&quot;style=\&quot;</span>background-color<span style="color: #006600; font-weight: bold;">:</span>#CCDDCC\<span style="color: #cc0000;">&quot;&quot;</span> <span style="color: #006600; font-weight: bold;">:</span> <span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span> 
  <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>
Here we&#8217;re specifying that the <strong>View Person</strong> column is a custom column that should be rendered using a Partial View, and the start of every alternate row has a background colour**.
</p>
<p>
Here is the same grid defined using the ActionSyntax:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model<span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Columns</span><span style="color: #006600; font-weight:bold;">&#40;</span>column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Named</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Person ID&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Gender</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
                column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;View Person&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Named</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Action</span><span style="color: #006600; font-weight:bold;">&#40;</span>p <span style="color: #006600; font-weight: bold;">=&gt;</span>	<span style="color: #006600; font-weight:bold;">&#123;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
                     &lt;td style=&quot;font-weight:bold&quot;&gt;
	                <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">ActionLink</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;View Person&quot;</span>, <span style="color: #cc0000;">&quot;Show&quot;</span>, <span style="color: #0000ff; font-weight: bold;">new</span> <span style="color: #006600; font-weight:bold;">&#123;</span> id <span style="color: #006600; font-weight: bold;">=</span> p.<span style="color: #9900cc;">Id</span> <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #000000; font-weight: bold;">%&gt;</span>
	             &lt;/td&gt;
                <span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     	<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">RowStart</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#40;</span>p,row<span style="color: #006600; font-weight:bold;">&#41;</span>  <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>     
             <span style="color: #990099; font-weight: bold;">if</span> <span style="color: #006600; font-weight:bold;">&#40;</span>row.<span style="color: #9900cc;">IsAlternate</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight:bold;">&#123;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
                   &lt;tr style=&quot;background-color:#CCDDCC&quot;&gt;
             <span style="color: #000000; font-weight: bold;">&lt;%</span>  <span style="color: #006600; font-weight:bold;">&#125;</span>  <span style="color: #990099; font-weight: bold;">else</span>  <span style="color: #006600; font-weight:bold;">&#123;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
                 &lt;tr&gt;
             <span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #006600; font-weight:bold;">&#125;</span>
    <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Render</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>
Note the following differences:
</p>
<ul>
<li>The ActionSyntax requires the use of &lt;% %&gt; tags rather than &lt;%= %&gt; tags.</li>
<li>The ActionSyntax requires that you end your grid definition with a call to the Render method or <strong>the grid will not show up if you do not call Render.</strong></li>
<li>You must terminate the call to Render with a semicolon</li>
<li>You can directly embed HTML in the grid definition rather than using strings or partial views</li>
</ul>
<p>
Both syntaxes have their advantages and disadvantages but the end result is the same. You&#8217;re free to use whichever one you prefer.
</p>
<p>
**Both the partial view/actionsyntax approach are designed only for complex column definitions. This example was quite contrived and you could achieve the same result with a far simpler approach:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;">&lt;style type=&quot;text/css&quot;&gt;
.gridrow_alternate { background-color:#CCDDCC; }
&lt;/style&gt;
<span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model<span style="color: #006600; font-weight:bold;">&#41;</span>
	.<span style="color: #9900cc;">Columns</span><span style="color: #006600; font-weight:bold;">&#40;</span>column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
  		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Named</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Person ID&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Gender</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>    
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Format</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;{0:d}&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> Html.<span style="color: #9900cc;">ActionLink</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;View Person&quot;</span>, <span style="color: #cc0000;">&quot;Show&quot;</span>, <span style="color: #0000ff; font-weight: bold;">new</span> <span style="color: #006600; font-weight:bold;">&#123;</span> id <span style="color: #006600; font-weight: bold;">=</span> p.<span style="color: #9900cc;">Id</span> <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
                           .<span style="color: #9900cc;">Attributes</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">new</span> Hash<span style="color: #006600; font-weight:bold;">&#40;</span>style <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #cc0000;">&quot;font-weight:bold&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     	<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>MvcContrib Grid Part 4 &#8211; Limitations of the WebFormsViewEngine</title>
		<link>http://www.jeremyskinner.co.uk/2009/02/28/mvccontrib-grid-part-4-limitations-of-the-webformsviewengine/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/02/28/mvccontrib-grid-part-4-limitations-of-the-webformsviewengine/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 15:58:51 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=106</guid>
		<description><![CDATA[This is part 4 of a series about the new MvcContrib grid.

Part 1 &#8211; Introduction
Part 2 &#8211; New Syntax
Part 3 &#8211; GridModels and GridRenderes
Part 4 &#8211; Limitations of the WebFormsViewEngine

Part 5 &#8211; The Action Syntax



I was recently asked a question by one of the other MvcContrib comitters, Will Shaver, about why the ToString method of [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 4 of a series about the new MvcContrib grid.</p>
<ul>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/">Part 2 &#8211; New Syntax</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers">Part 3 &#8211; GridModels and GridRenderes</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/28/mvccontrib-grid-part-4-limitations-of-the-webformsviewengine">Part 4 &#8211; Limitations of the WebFormsViewEngine</a></li>
<li>
<a href="http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax">Part 5 &#8211; The Action Syntax</a>
</li>
</ul>
<p>
I was recently asked a question by one of the other MvcContrib comitters, Will Shaver, about why the <strong>ToString</strong> method of the Grid does not return the HTML for the grid, but instead writes the html its internal textwriter and then returns null.
</p>
<p>This is the offending piece of code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">string</span> ToString<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
<span style="color: #000000;">&#123;</span>
	_gridModel.<span style="color: #0000FF;">Renderer</span>.<span style="color: #0000FF;">Render</span><span style="color: #000000;">&#40;</span>_gridModel, DataSource, _writer, context<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
This means that when ToString is called on the grid the GridRenderer will render the appropriate HTML to a textwriter (which is actually the HTTP Response output stream). From the end user&#8217;s perspective, this shouldn&#8217;t make a difference because it &#8216;appears&#8217; that the grid is being rendered at the correct location:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Columns</span><span style="color: #006600; font-weight:bold;">&#40;</span>...<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>
ToString is implicitly called at the end of the &lt;%= %&gt; block which causes the rendering to occur.
</p>
<h2>What a silly way to create some html!</h2>
<p>
Yes, it may seem like a strange way of doing things but there is actually a good reason for this. In order to support rendering Partials for parts of the grid it is necessary that the grid is <strong>written directly to the response stream</strong> due to limitations of the WebForms View Engine.
</p>
<h2>What about passing a custom TextWriter to IView.Render?</h2>
<p>
The ASP.NET MVC IView represents the view that will be rendered. Take a look at the signature for its Render method:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">void</span> Render<span style="color: #000000;">&#40;</span>ViewContext viewContext, TextWriter writer<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>
You might think that it would be possible to pass a StringWriter to the Render method, capture the output of the partial view and then store it internally in the grid. Unfortunately, this isn&#8217;t possible. I&#8217;m guessing the TextWriter parameter here is a hook for a future extensibility point &#8211; the current implementation of WebFormView <strong>completely ignores the parameter.</strong> In fact, internally WebFormView has to make use of HttpContext.Current in order to work around the limitation of .aspx/web forms.
</p>
<h2>What about the BlockRenderer?</h2>
<p>
MvcContrib contains a class called the <a href="http://mvccontrib.googlecode.com/svn/trunk/src/MVCContrib/UI/BlockRenderer.cs">BlockRenderer</a> that can be used to capture the outputs of a view by making use of a Response Filter. While I could have used this approach, it suffers from several major flaws. In order for this approach to work, it is necessary to Flush the response stream before inserting the CapturingResponseFilter. This causes problems if you&#8217;re trying to do anything funky with HTTP Headers, or even something as simple as trying to set the Content Type. Check out <a href="http://forums.asp.net/p/1376598/2896402.aspx">this post on the MVC Forums</a> for more details. There are other hacks that can be used, but none of them are ideal
</p>
<p>
In the end I decided the least evil approach was to render to Response.Output from within ToString as it causes the fewest problems for the end user. Hopefully with the release of ASP.NET 4.0 the MVC Team will be allowed to make some changes to the underlying WebForms engine in order to make it possible to capture view output.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/02/28/mvccontrib-grid-part-4-limitations-of-the-webformsviewengine/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rewriting the MvcContrib Grid part 3 &#8211; GridModels and GridRenderers</title>
		<link>http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers/#comments</comments>
		<pubDate>Mon, 23 Feb 2009 18:47:02 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=98</guid>
		<description><![CDATA[This is part 3 of a series about the new MvcContrib grid.

Part 1 &#8211; Introduction
Part 2 &#8211; New Syntax
Part 3 &#8211; GridModels and GridRenderes
Part 4 &#8211; Limitations of the WebFormsViewEngine

Part 5 &#8211; The Action Syntax



Internally, the new MvcContrib grid makes use of the GridModel and GridRenderer classes to create the grid definition and then render [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 3 of a series about the new MvcContrib grid.</p>
<ul>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/">Part 2 &#8211; New Syntax</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers">Part 3 &#8211; GridModels and GridRenderes</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/28/mvccontrib-grid-part-4-limitations-of-the-webformsviewengine">Part 4 &#8211; Limitations of the WebFormsViewEngine</a></li>
<li>
<a href="http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax">Part 5 &#8211; The Action Syntax</a>
</li>
</ul>
<p>
Internally, the new MvcContrib grid makes use of the <strong>GridModel</strong> and <strong>GridRenderer</strong> classes to create the grid definition and then render it. It is possible to make use of these classes yourself to customise the grid.
</p>
<h2>Grid Models</h2>
<p>The GridModel stores the definition for the grid. That is, it contains column definitions, custom attributes and the renderer to use (see below). Each time you call a method on the Grid class it is delegated to the underlying GridModel. For example, calling the <strong>Columns</strong> defines a collection of GridColumn objects which are instantiated and stored in the GridModel:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Columns</span><span style="color: #006600; font-weight:bold;">&#40;</span>column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
               column <span style="color: #006600; font-weight: bold;">=&gt;</span> column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
               column <span style="color: #006600; font-weight: bold;">=&gt;</span> column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>
Instead of defining columns in your view, you can create your own GridModel and then pass this object to a call to Html.Grid:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PersonGridModel <span style="color: #008000;">:</span> GridModel<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> PersonGridModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        Column.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Column.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">DateOfBirth</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">WithModel</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> PersonGridModel<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>
Note that GridModels aren&#8217;t limited just to column definitions. Everything that you could invoke by calling Html.Grid has an equivalent method on the GridModel (eg setting Empty Text, custom section overrides, header attributes etc).
</p>
<p>
There are several advantages of this approach:
</p>
<ul>
<li>If you have large grid definitions using a GridModel can help to keep the view uncluttered.</li>
<li>It is easy to re-use the same grid in multiple locations.</li>
<li>You can re-use common grid code through inheritance.</li>
</ul>
<p>
There is one other benefit to using a GridModel. Visual Basic 2008 doesn&#8217;t support Lambda Statements meaning that it is not possible to use the Columns() method on the Grid directly. By using a grid model then you can still make use of the grid from within VB (if you&#8217;re that way inclined).
</p>
<h2>Grid Renderers</h2>
<p>Internally, it is the job of the GridRenderer to take a GridModel and render it to the output stream. The default GridRenderer is the <strong>HtmlTableGridRenderer</strong>. Which, surprisingly enough, renders the grid as an HTML table. By specifying your own GridRenderer, you can completely alter how the grid is rendered without resorting to completely rewriting the grid component.</p>
<p>
For example, in one of my larger ASP.NET MVC applications I frequently have the need to output data as an Excel spreadsheet and I used the grid to do so. In order to make this work I had to inherit from GridBase and override the methods related to generating output and then define a new set of helper methods to cater for the various Grid overloads. In the end I ended up with several overloads like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExcelGrid<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper helper, <span style="color: #FF0000;">string</span> viewDataKey, Action<span style="color: #008000;">&lt;</span>IRootGridColumnBuilder<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> columns<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExcelGrid<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper helper, <span style="color: #FF0000;">string</span> viewDataKey, Action<span style="color: #008000;">&lt;</span>IRootGridColumnBuilder<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> columns, Action<span style="color: #008000;">&lt;</span>IGridSections<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> sections<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExcelGrid<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper helper, <span style="color: #FF0000;">string</span> viewDataKey, IDictionary htmlAttributes, Action<span style="color: #008000;">&lt;</span>IRootGridColumnBuilder<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> columns<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExcelGrid<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper helper, <span style="color: #FF0000;">string</span> viewDataKey, IDictionary htmlAttributes, Action<span style="color: #008000;">&lt;</span>IRootGridColumnBuilder<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> columns, Action<span style="color: #008000;">&lt;</span>IGridSections<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> sections<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExcelGrid<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper helper, IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> dataSource, Action<span style="color: #008000;">&lt;</span>IRootGridColumnBuilder<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> columns<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExcelGrid<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper helper, IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> dataSource, Action<span style="color: #008000;">&lt;</span>IRootGridColumnBuilder<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> columns, Action<span style="color: #008000;">&lt;</span>IGridSections<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> sections<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExcelGrid<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper helper, IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> dataSource, IDictionary htmlAttributes, Action<span style="color: #008000;">&lt;</span>IRootGridColumnBuilder<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> columns<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> 
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ExcelGrid<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span> HtmlHelper helper, IEnumerable<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> dataSource, IDictionary htmlAttributes, Action<span style="color: #008000;">&lt;</span>IRootGridColumnBuilder<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> columns, Action<span style="color: #008000;">&lt;</span>IGridSections<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span> sections<span style="color: #000000;">&#41;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span></pre></div></div>

<p>&#8230;which was largely a duplication of the Grid overloads in mvccontrib. They could then be used like this:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span> Html.<span style="color: #9900cc;">ExcelGrid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span>, column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
         column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
         column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
 <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>&#8230;which would produce the grid as Excel-XML.</p>
<p>With the new grid I can create a new GridRenderer implementation and then use it like this, with no need to create additional HtmlHelper extensions:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Column</span><span style="color: #006600; font-weight:bold;">&#40;</span>column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
               column <span style="color: #006600; font-weight: bold;">=&gt;</span> column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
               column <span style="color: #006600; font-weight: bold;">=&gt;</span> column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">RenderUsing</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> ExcelGridRenderer<span style="color: #006600; font-weight: bold;">&lt;</span>Person<span style="color: #006600; font-weight: bold;">&gt;</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span></pre></div></div>

<p>The ExcelGridRenderer<T> could inherit from GridRenderer<T> and override the methods needed for constructing the grid header, footer, rows etc. You may notice that the methods on GridRenderer are very similar to those on the GridBase class. This is not a coincidence &#8211; most of the code in the GridRenderer is re-used from the old GridBase.</p>
<p>Note it is also possible to specify the renderer from within a GridModel:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PersonGridModel <span style="color: #008000;">:</span> GridModel<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> PersonGridModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        Column.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        Column.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">DateOfBirth</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        RenderUsing<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ExcelGridRenderer<span style="color: #008000;">&lt;</span>Person<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Hopefully this brief introduction to GridRenderers and GridModels shows how it is possible to make your grids extendable and reusable. <del datetime="2009-02-28T16:00:02+00:00">In my next post I&#8217;m going to focus on Paging in more detail.</del> My next post covers working around some of the issues of the WebForms View Engine.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Rewriting the MvcContrib Grid part 2 &#8211; New Syntax</title>
		<link>http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 13:40:17 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=91</guid>
		<description><![CDATA[
This is part two of a series on the new MvcContrib grid component.


Part 1 &#8211; Introduction
Part 2 &#8211; New Syntax
Part 3 &#8211; GridModels and GridRenderes
Part 4 &#8211; Limitations of the WebFormsViewEngine

Part 5 &#8211; The Action Syntax



Edit: 28 Feb 09 I have removed the use of Partial views in the latest MvcContrib trunk for certain areas [...]]]></description>
			<content:encoded><![CDATA[<p>
This is part two of a series on the new MvcContrib grid component.
</p>
<ul>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/">Part 2 &#8211; New Syntax</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers">Part 3 &#8211; GridModels and GridRenderes</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/28/mvccontrib-grid-part-4-limitations-of-the-webformsviewengine">Part 4 &#8211; Limitations of the WebFormsViewEngine</a></li>
<li>
<a href="http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax">Part 5 &#8211; The Action Syntax</a>
</li>
</ul>
<p>
<strong>Edit: 28 Feb 09</strong> I have removed the use of Partial views in the latest MvcContrib trunk for certain areas where it didn&#8217;t really make sense. The examples below have been updated to reflect this.
</p>
<p>
The updated version of the MvcContrib grid has a new syntax that uses method chaining and lambda expressions to build a fluent interface. Here is a grid defined with the new syntax:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Columns</span><span style="color: #006600; font-weight:bold;">&#40;</span>column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Named</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Person ID&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Format</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;{0:d}&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     	<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
        .<span style="color: #9900cc;">Attributes</span><span style="color: #006600; font-weight:bold;">&#40;</span>style <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #cc0000;">&quot;width:100%&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
     	.<span style="color: #9900cc;">Empty</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;There are no people.&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
     	.<span style="color: #9900cc;">RowStart</span><span style="color: #006600; font-weight:bold;">&#40;</span>row <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #cc0000;">&quot;&lt;tr foo='bar'&gt;&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>Compare this to the same grid defined using the old syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span>
  Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span>,
    <span style="color: #0000ff; font-weight: bold;">new</span> Hash<span style="color: #006600; font-weight:bold;">&#40;</span>empty <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #cc0000;">&quot;There are no people&quot;</span>, style <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #cc0000;">&quot;width:100%&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>,
    column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
	column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span>, <span style="color: #cc0000;">&quot;Person Id&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     	column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     	column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Format</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;{0:d}&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
    <span style="color: #006600; font-weight:bold;">&#125;</span>, sections <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
       sections.<span style="color: #9900cc;">RowStart</span><span style="color: #006600; font-weight:bold;">&#40;</span>item <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
          &lt;tr&gt; &lt;!-- some custom html for the row start here --&gt;
   <span style="color: #000000; font-weight: bold;">&lt;%</span> <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
<span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>
Note how grid options (eg Empty) are now specified as part of the fluent interface rather than part of the HTML Attributes dictionary (which I always felt was messy). The use of method chaining also means that it is no longer possible to embed html blocks inside the grid definition (eg RowStart). However, you can still achieve the same functionality using the <strong>RowStart</strong> method and specifying specifying a lambda that will return the HTML string to output.
</p>
<p>The column definitions are almost identical identical.</p>
<h2>Paging support</h2>
<p>
Unlike its predecessor, the new grid does not have any logic for dealing with paging. This has all been moved to a separate <strong>Pager</strong> component. The old grid rendered the pager in its footer, while with the new grid you&#8217;d need to write your page like this:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Grid</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Columns</span><span style="color: #006600; font-weight:bold;">&#40;</span>column <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Id</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Named</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Person ID&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
     		column.<span style="color: #990099; font-weight: bold;">For</span><span style="color: #006600; font-weight:bold;">&#40;</span>x <span style="color: #006600; font-weight: bold;">=&gt;</span> x.<span style="color: #9900cc;">DateOfBirth</span><span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">Format</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;{0:d}&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">Pager</span><span style="color: #006600; font-weight:bold;">&#40;</span>Model.<span style="color: #9900cc;">People</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<h2>Where&#8217;s the code?</h2>
<p>
The code for the new grid is not in the mvccontrib release, so if you want to play with it you&#8217;ll need to download and build <a href="http://mvccontrib.googlecode.com/svn/trunk">the source</a>. Alternatively, I&#8217;ve put together a custom build that can be <a href="http://www.jeremyskinner.co.uk/files/mvccontrib-r829.zip">downloaded here</a> for the subversion-impaired.
</p>
<p>
In my next post, I&#8217;ll be focussing on extending the grid by using GridModels and GridRenderers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Rewriting the MvcContrib Grid</title>
		<link>http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/</link>
		<comments>http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 13:47:44 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=88</guid>
		<description><![CDATA[This is part 1 of a series on the new MvcContrib grid component.

Part 1 &#8211; Introduction
Part 2 &#8211; New Syntax
Part 3 &#8211; GridModels and GridRenderes
Part 4 &#8211; Limitations of the WebFormsViewEngine

Part 5 &#8211; The Action Syntax


Over the next couple of weeks I&#8217;m hoping to write a few posts on the upgrades I&#8217;m doing to the [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 1 of a series on the new MvcContrib grid component.</p>
<ul>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/">Part 1 &#8211; Introduction</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/">Part 2 &#8211; New Syntax</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/23/rewriting-the-mvccontrib-grid-part-3-gridmodels-and-gridrenderers">Part 3 &#8211; GridModels and GridRenderes</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/28/mvccontrib-grid-part-4-limitations-of-the-webformsviewengine">Part 4 &#8211; Limitations of the WebFormsViewEngine</a></li>
<li>
<a href="http://www.jeremyskinner.co.uk/2009/03/01/mvccontrib-grid-part-5-the-action-syntax">Part 5 &#8211; The Action Syntax</a>
</li>
</ul>
<p>Over the next couple of weeks I&#8217;m hoping to write a few posts on the upgrades I&#8217;m doing to the <a href="http://www.codeplex.com/MVCContrib/Wiki/View.aspx?title=Grid&#038;referringTitle=Documentation">MvcContrib grid component.</a> These are the features that I&#8217;m planning on adding:
</p>
<h2>A Fluent Interface</h2>
<p>
Like the HTML-helpers built into the ASP.NET MVC framework, the mvccontrib grid suffers from &#8216;overload hell&#8217;. Switching to a fluent interface should provide a cleaner and more discoverable API. </p>
<h2>Discouraging logic in Views</h2>
<p>
One side effect of the fluent interface is that it will no longer be possible to use the grid&#8217;s section overrides. However, I consider this to be a good thing. Embedding custom sections makes it all to easy to get stuck in &#8216;tag soup&#8217;. To make up for this, the new grid will support rendering partials for custom sections.
</p>
<h2>Reusable Grid Models</h2>
<p>
The new grid will support reusable grid definitions. That is, you will be able to define a grid in a external class and then reference this from your views. One benefit of this approach is that it means the grid will finally be accessible to VB users. The current grid is C#-only due to its use of lambda-statements for defining columns.
</p>
<h2>Customisable Grid Rendering</h2>
<p>
The existing grid only supports rendering in an HTML table. This means that if you wanted to render a grid in a different format (for example, as an OpenXML Spreadsheet) it meant you needed to inherit from the Grid class, override several methods and then create a new helper. The new grid will allow a custom GridRenderer class to be provided in the grid definition. Rendering as an HTML table will still be the default behaviour.
</p>
<h2>Separate Pagination Component</h2>
<p>The current grid has built in support for paging. With the new grid, this will be moved into a separate component.</p>
<h2>Sorting</h2>
<p>Time permitting, the new grid will have support for sorting.</p>
<p>In my next post I&#8217;ll focus on the grid&#8217;s Fluent Interface.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Using ModelState with MvcContrib&#8217;s Fluent HTML Helpers</title>
		<link>http://www.jeremyskinner.co.uk/2008/12/18/using-modelstate-with-mvccontribs-fluent-html-helpers/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/12/18/using-modelstate-with-mvccontribs-fluent-html-helpers/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 12:11:04 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[mvccontrib]]></category>
		<category><![CDATA[aspnetmvc]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=71</guid>
		<description><![CDATA[What is ModelState?

The ASP.NET MVC ModelState dictionary allows you to record validation errors which can then be displayed in your web page.

For example, imagine you add a textbox to a page using the MVC framework&#8217;s built-in HTML helpers, then this will render a standard text field:


Your name: &#60;%= Html.TextBox&#40;&#34;Name&#34;&#41; %&#62;

In your controller, if you add [...]]]></description>
			<content:encoded><![CDATA[<h3>What is ModelState?</h3>
<p>
The ASP.NET MVC ModelState dictionary allows you to record validation errors which can then be displayed in your web page.
</p>
<p>For example, imagine you add a textbox to a page using the MVC framework&#8217;s built-in HTML helpers, then this will render a standard text field:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;">Your name: <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Name&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>In your controller, if you add an error to the ModelState dictionary with the same name as the textbox, then the HTML helper will change the styling of the input field by adding a css class of &#8220;input-validation-error&#8221;. </p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HomeController <span style="color: #008000;">:</span> Controller <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> ActionResult Index<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
       ModelState.<span style="color: #0000FF;">AddModelError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span>, <span style="color: #666666;">&quot;Please enter a name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> MyViewModel <span style="color: #000000;">&#123;</span> Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Using the default ASP.NET MVC template, it will add a red border with a light pink background:</p>
<p><img src="/files/textbox-error.png" alt="Textbox error" style="border:1px solid #bebebe" /></p>
<p>
You can also display the actual validation errors by making use of the ValidationSummary helper:
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">ValidationSummary</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
Enter your name: <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> Html.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Name&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>Which will display something like this:</p>
<p><img src="/files/textbox-validationsummary.png" style="border:1px solid #bebebe" alt="Validation Summary" /></p>
<h3>ModelState with FluentHtml</h3>
<p>
By default, the Fluent HTML helpers in MvcContrib do not have support for ModelState. However, it is very easy to add support for this by writing a custom MemberBehaviour (I previously wrote about extending the HTML helpers with MemberBehaviors  <a href="http://www.jeremyskinner.co.uk/2008/12/13/integrating-fluentvalidation-with-mvccontribs-fluent-html-helpers/">here</a>)
</p>
<p>
Here is the implementation of the ModelStateMemberBehavior class:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ModelStateMemberBehavior <span style="color: #008000;">:</span> IMemberBehavior <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> ModelStateDictionary modelState<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> ModelStateMemberBehavior<span style="color: #000000;">&#40;</span>ModelStateDictionary modelState<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">modelState</span> <span style="color: #008000;">=</span> modelState<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Execute<span style="color: #000000;">&#40;</span>IMemberElement element<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		ModelState state<span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">Attributes</span>.<span style="color: #0000FF;">ContainsKey</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;name&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>modelState.<span style="color: #0000FF;">TryGetValue</span><span style="color: #000000;">&#40;</span>element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">Attributes</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;name&quot;</span><span style="color: #000000;">&#93;</span>, <span style="color: #0600FF;">out</span> state<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
				element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">AddCssClass</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;input-validation-error&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This class will check whether the name of the element being rendered is present in the specified ModelState dictionary. If so, it will add a css class of &#8220;input-validation-error&#8221; to the element being rendered.</p>
<p>
Now we need to hook this into the HTML helpers. This can be done by implementing the IViewModelContainer interface on your base View Page:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyBaseViewPage<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> ViewPage<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, IViewModelContainer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">private</span> List<span style="color: #008000;">&lt;</span>IMemberBehavior<span style="color: #008000;">&gt;</span> memberBehaviors <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>IMemberBehavior<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> SetViewData<span style="color: #000000;">&#40;</span>ViewDataDictionary viewData<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">SetViewData</span><span style="color: #000000;">&#40;</span>viewData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      behaviors.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ModelStateMemberBehavior<span style="color: #000000;">&#40;</span>ViewData.<span style="color: #0000FF;">ModelState</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">public</span> T ViewModel <span style="color: #000000;">&#123;</span>
      get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> ViewData.<span style="color: #0000FF;">Model</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">public</span> IEnumerable<span style="color: #008000;">&lt;</span>IMemberBehavior<span style="color: #008000;">&gt;</span> MemberBehaviors <span style="color: #000000;">&#123;</span>
	get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> memberBehaviors<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Now, so long as all your view pages inherit from MyBaseViewPage<T>, any calls to the fluent HTML helpers will have support for ModelState:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;">Your name: <span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> this.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold;">&#40;</span>model <span style="color: #006600; font-weight: bold;">=&gt;</span> model.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/12/18/using-modelstate-with-mvccontribs-fluent-html-helpers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Integrating FluentValidation with MvcContrib&#8217;s Fluent Html Helpers</title>
		<link>http://www.jeremyskinner.co.uk/2008/12/13/integrating-fluentvalidation-with-mvccontribs-fluent-html-helpers/</link>
		<comments>http://www.jeremyskinner.co.uk/2008/12/13/integrating-fluentvalidation-with-mvccontribs-fluent-html-helpers/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 13:00:21 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[FluentValidation]]></category>
		<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/?p=66</guid>
		<description><![CDATA[A recent addition to the MvcContrib project are a set of HTML helpers that use a fluent interface written by Tim Scott.


One of the great things about these helpers is that you can change how the HTML is rendered by writing custom IMemberBehavior objects. For example, the helpers come with a set of attributes that [...]]]></description>
			<content:encoded><![CDATA[<p>A recent addition to the <a href="http://mvccontrib.org">MvcContrib</a> project are a set of HTML helpers that use a fluent interface written by <a href="http://lunaverse.wordpress.com/2008/11/24/mvcfluenthtml-fluent-html-interface-for-ms-mvc/">Tim Scott.</a>
</p>
<p>
One of the great things about these helpers is that you can change how the HTML is rendered by writing custom IMemberBehavior objects. For example, the helpers come with a set of attributes that you can use to decorate your model:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Customer <span style="color: #000000;">&#123;</span>
   <span style="color: #000000;">&#91;</span>MaxLength<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
   <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Then, when you render your view, the HTML helper will automatically detect the presence of this attribute and render a &#8220;maxlength&#8221; attribute accordingly. For example, this textbox helper&#8230;
</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">=</span> this.<span style="color: #9900cc;">TextBox</span><span style="color: #006600; font-weight:bold;">&#40;</span>m <span style="color: #006600; font-weight: bold;">=&gt;</span> m.<span style="color: #9900cc;">Customer</span>.<span style="color: #9900cc;">Name</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>&#8230;would render this html:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">maxlength</span>=<span style="color: #ff0000;">&quot;50&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;Customer_Name&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Customer.Name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>
I wanted to take this a step further and integrate it with <a href="http://codeplex.com/FluentValidation">my validation library</a>, so the HTML helpers will automatically pick up maxlength/required attributes from my validator classes.
</p>
<h3>Step 1: Custom Validator Class</h3>
<p>The first stage was to create a metadata class to hold information about a validator.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PropertyModel <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> MaxLength <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> Required <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">public</span> PropertyInfo Property <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF;">private</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> PropertyModel<span style="color: #000000;">&#40;</span>PropertyInfo property<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		Property <span style="color: #008000;">=</span> property<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Next, I introduced a new interface, IPropertyDescriptor, that all my validator classes will implement. This interface defines the signature for a method that creates PropertyModel instances based on a PropertyInfo:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">interface</span> IPropertyDescriptor <span style="color: #000000;">&#123;</span>
	PropertyModel GetPropertyModel<span style="color: #000000;">&#40;</span>PropertyInfo property<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Next, I added a custom base-class for all my validators which implements the above interface:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> abstract <span style="color: #FF0000;">class</span> Validator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> AbstractValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span>, IPropertyDescriptor <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> PropertyModel GetPropertyModel<span style="color: #000000;">&#40;</span>PropertyInfo property<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var model <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PropertyModel<span style="color: #000000;">&#40;</span>property<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		var validators <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">OfType</span><span style="color: #008000;">&lt;</span>IPropertyValidatorContainer<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Property</span> <span style="color: #008000;">==</span> property<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> x.<span style="color: #0000FF;">Validator</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>var validator <span style="color: #0600FF;">in</span> validators<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HandleLengthValidator<span style="color: #000000;">&#40;</span>validator, model<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> continue<span style="color: #008000;">;</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>HandleRequiredValidator<span style="color: #000000;">&#40;</span>validator, model<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> continue<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF;">return</span> model<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> HandleLengthValidator<span style="color: #000000;">&#40;</span>IPropertyValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> validator, PropertyModel model<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var length <span style="color: #008000;">=</span> validator <span style="color: #0600FF;">as</span> ILengthValidator<span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>length <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			model.<span style="color: #0000FF;">MaxLength</span> <span style="color: #008000;">=</span> length.<span style="color: #0000FF;">Max</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> HandleRequiredValidator<span style="color: #000000;">&#40;</span>IPropertyValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> validator, PropertyModel model<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var required <span style="color: #008000;">=</span> validator <span style="color: #0600FF;">as</span> INotNullValidator<span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>required <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
			model.<span style="color: #0000FF;">Required</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
			<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		<span style="color: #0600FF;">return</span> false<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>	
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Essentially, the GetPropertyModel method iterates through all of the property validators to see if any required or max-length validators have been defined. If so, it records them in the PropertyModel object.
</p>
<p>I can now define my validators as usual, but they now inherit from my new Validator&lt;T&gt; class, rather than AbstractValidator&lt;T&gt;.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> CustomerValidator <span style="color: #008000;">:</span> Validator<span style="color: #008000;">&lt;</span>Customer<span style="color: #008000;">&gt;</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> CustomerValidator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     RuleFor<span style="color: #000000;">&#40;</span>customer <span style="color: #008000;">=&gt;</span> customer.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>Step 2: Custom Validator Factory</h3>
<p>
The next stage is to impelement a custom validator factory that can be used to instantiate validators. For this example, I&#8217;ll be using the <a href="http://castleproject.org">Windsor</a> IoC container.
</p>
<p>
Firstly, in my application startup routine I need to register all of my validators with Windsor:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var container <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WindsorContainer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
container.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>AllTypes.<span style="color: #0000FF;">Of</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>IValidator<span style="color: #008000;">&lt;&gt;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">FromAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>CustomerValidator<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Assembly</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">WithService</span>.<span style="color: #0600FF;">Base</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Next, I create a validator factory that uses Windsor (or more accurently, Microkernel) to create the validator instances.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ValidatorFactory <span style="color: #008000;">:</span> IValidatorFactory <span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IKernel container<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> ValidatorFactory<span style="color: #000000;">&#40;</span>IKernel container<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">container</span> <span style="color: #008000;">=</span> container<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> GetValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> container.<span style="color: #0000FF;">TryResolve</span><span style="color: #008000;">&lt;</span>IValidator<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> IValidator GetValidator<span style="color: #000000;">&#40;</span>Type type<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
		var genericType <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>IValidator<span style="color: #008000;">&lt;&gt;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">MakeGenericType</span><span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>IValidator<span style="color: #000000;">&#41;</span>container.<span style="color: #0000FF;">TryResolve</span><span style="color: #000000;">&#40;</span>genericType<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>
Note that TryResolve is an extension method on IKernel that will attept to resolve a component and return null if it fails (by default, Windsor/Microkernel will throw an exception).
</p>
<p>
This validator factory can now be registered with the container:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">container.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>Component.<span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>IValidatorFactory<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ImplementedBy</span><span style="color: #008000;">&lt;</span>ValidatorFactory<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h3>
Step 3: The PropertyModelFactory<br />
</h3>
<p>The next stage is to create a class that can be used to locate the correct validator from a PropertyInfo object (by using the validator factory) and return a corresponding PropertyModel.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> PropertyModelFactory <span style="color: #008000;">:</span> IPropertyDescriptor <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">private</span> IValidatorFactory validatorFactory<span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #0600FF;">public</span> PropertyModelFactory<span style="color: #000000;">&#40;</span>IValidatorFactory validatorFactory<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">validatorFactory</span> <span style="color: #008000;">=</span> validatorFactory<span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">public</span> PropertyModel GetPropertyModel<span style="color: #000000;">&#40;</span>PropertyInfo property<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
     var descriptor <span style="color: #008000;">=</span> validatorFactory.<span style="color: #0000FF;">GetValidator</span><span style="color: #000000;">&#40;</span>property.<span style="color: #0000FF;">ReflectedType</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">as</span> IPropertyDescriptor<span style="color: #008000;">;</span>
     <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>descriptor <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> descriptor.<span style="color: #0000FF;">GetPropertyModel</span><span style="color: #000000;">&#40;</span>property<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
     <span style="color: #000000;">&#125;</span>
     <span style="color: #0600FF;">return</span> null<span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Again, this needs to be registered with the container:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">container.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>Component.<span style="color: #0600FF;">For</span><span style="color: #008000;">&lt;</span>IPropertyDescriptor<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ImplementedBy</span><span style="color: #008000;">&lt;</span>PropertyModelFactory<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<h3>Step 4: Custom IMemberBehavior</h3>
<p>
Next, we need to create a custom IMemberBehavior that will make use of the PropertyModelFactory:
</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ValidatorMemberBehaviour <span style="color: #008000;">:</span> IMemberBehavior <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">readonly</span> IPropertyDescriptor descriptor<span style="color: #008000;">;</span>
&nbsp;
   <span style="color: #0600FF;">public</span> ValidatorMemberBehaviour<span style="color: #000000;">&#40;</span>IPropertyDescriptor descriptor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">descriptor</span> <span style="color: #008000;">=</span> descriptor<span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Execute<span style="color: #000000;">&#40;</span>IMemberElement element<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>element.<span style="color: #0000FF;">ForMember</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> return<span style="color: #008000;">;</span>
&nbsp;
      var property <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">ForMember</span>.<span style="color: #0000FF;">Member</span> <span style="color: #0600FF;">as</span> PropertyInfo<span style="color: #008000;">;</span>
      <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>property <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> return<span style="color: #008000;">;</span>
&nbsp;
      var model <span style="color: #008000;">=</span> descriptor.<span style="color: #0000FF;">GetPropertyModel</span><span style="color: #000000;">&#40;</span>property<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>model <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> return<span style="color: #008000;">;</span>
&nbsp;
      var maxLengthMethod <span style="color: #008000;">=</span> element.<span style="color: #0000FF;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">GetMethod</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;MaxLength&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>maxLengthMethod <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
         element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">MergeAttribute</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;maxlength&quot;</span>, model.<span style="color: #0000FF;">MaxLength</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>				
      <span style="color: #000000;">&#125;</span>
      <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>model.<span style="color: #0000FF;">Required</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
         element.<span style="color: #0000FF;">Builder</span>.<span style="color: #0000FF;">AddCssClass</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;required&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>MemberBehaviors are invoked when certain HTML helper objects are created, so this behaviour now needs to be registered with our ViewPage:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyViewPage<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> <span style="color: #008000;">:</span> ModelViewPage<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;</span> where T <span style="color: #008000;">:</span> <span style="color: #FF0000;">class</span> <span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">public</span> MyViewPage<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
     <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> ValidatorMemberBehaviour<span style="color: #000000;">&#40;</span>ServiceLocator.<span style="color: #0000FF;">Resolve</span><span style="color: #008000;">&lt;</span>IPropertyDescriptor<span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note that the ServicLocator static class is just a wrapper around Windsor.</p>
<p>So now the HTML Helpers will use my validator definitions to generate required/maxlength attributes. I think the extensibility of these helpers is incredibly powerful and I am planning on using them going forward in my next project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2008/12/13/integrating-fluentvalidation-with-mvccontribs-fluent-html-helpers/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
