<?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>On Error Resume Coding</description>
	<lastBuildDate>Sun, 08 Aug 2010 13:36:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>MvcContrib Grid Presentation</title>
		<link>http://www.jeremyskinner.co.uk/2010/04/28/mvccontrib-grid-presentation/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/04/28/mvccontrib-grid-presentation/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 18:14:00 +0000</pubDate>
		<dc:creator>Jeremy Skinner</dc:creator>
				<category><![CDATA[mvccontrib]]></category>

		<guid isPermaLink="false">http://www.jeremyskinner.co.uk/2010/04/28/mvccontrib-grid-presentation/</guid>
		<description><![CDATA[Today I did a presentation for the C4mvc virtual usergroup on the MvcContrib grid component.&#160; In the presentation I covered basic grid usage, auto-generated columns, paging, sorting and extensibility using custom grid renderers. The sample code that I used in the presentation is available here. The video for the talk is available on viddler.]]></description>
			<content:encoded><![CDATA[<p>Today I did a presentation for the <a href="http://c4mvc.net">C4mvc</a> virtual usergroup on the <a href="http://mvccontrib.codeplex.com/wikipage?title=Grid&amp;referringTitle=Documentation">MvcContrib grid component</a>.&#160; </p>
<p>In the presentation I covered basic grid usage, auto-generated columns, paging, sorting and extensibility using custom grid renderers. </p>
<p>The sample code that I used in the presentation <a href="http://github.com/downloads/JeremySkinner/Presentations/GridDemo-2010-04-28.zip">is available here</a>. The video for the talk <a href="http://www.viddler.com/explore/c4mvc/videos/38/">is available on viddler</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/04/28/mvccontrib-grid-presentation/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>MvcContrib Grid Part 7 &#8211; Auto-generated columns</title>
		<link>http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 09:18:39 +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/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/</guid>
		<description><![CDATA[This is part 7 of a series of posts on the MvcContrib grid component. Part 1 – Introduction Part 2 – New Syntax Part 3 – GridModels and GridRenderes Part 4 – Limitations of the WebFormsViewEngine Part 5 – The Action Syntax Part 6 – Sorting Part 7 – Auto-generated columns The MvcContrib grid provides [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 7 of a series of posts on the <a href="http://mvccontrib.codeplex.com/wikipage?title=Grid">MvcContrib grid component</a>.</p>
<ul>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/">Part 1 – Introduction</a> </li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/">Part 2 – 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 – 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 – 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 – The Action Syntax</a> </li>
<li><a href="http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/">Part 6 – Sorting</a> </li>
<li><a href="http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/">Part 7 – Auto-generated columns</a></li>
</ul>
<p>The MvcContrib grid provides a way to automatically generate columns based on the public properties of an object. This is particularly useful when you are using a dedicated <a href="http://martinfowler.com/eaaDev/PresentationModel.html">presentation model</a> where the object properties map directly to the columns that you want to display. </p>
<p>To make use of this feature, you can call the <strong>AutoGenerateColumns</strong> method in the Grid declaration:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:b51d0d81-c85d-4f64-ae16-23cde9daa90f" class="wlWriterEditableSmartContent">

<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;">AutoGenerateColumns</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>

</div>
<p>By default, this will generate one column per public property and the name of the property will be used as the column heading (although PascalCased properties will have spaces inserted, eg “DateOfBirth” would be rendered as “Date Of Birth”).</p>
<p>This output can be customised by making use of MVC2’s ModelMetadata.</p>
<p>Out of the box, MVC2 uses the System.ComponentModel.DataAnnotations attributes to obtain metadata about a particular type (although you can also use <a href="http://fluentvalidation.codeplex.com">other frameworks</a>). The following attributes can be used to customise the auto-generated columns:</p>
<ul>
<li>ScaffoldColumn – can be used to prevent a property from being rendered as a grid column</li>
<li>DisplayName – can be used to provide a custom column heading</li>
<li>DisplayFormat – can be used to specify a custom display format</li>
</ul>
<p>Additional columns not on your presentation model can also be added by making a call to the normal <strong>Columns</strong> method after the call to <strong>AutoGenerateColumns</strong>. For example, imagine we have a CustomerPresentationModel:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:86b1f7f8-b42e-49bd-aa1f-eb32a7b604bb" class="wlWriterEditableSmartContent">

<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> CustomerPresentationModel <span style="color: #000000;">&#123;</span>
&nbsp;
   <span style="color: #000000;">&#91;</span>ScaffoldColumn<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
   <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> CustomerId <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
   <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: #0600FF;">public</span> DateTime DateOfBirth <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>

</div>
<p>If a collection of these objects is passed to the Grid method, then two columns will be auto-generated – one for Name and one for DateOfBirth (CustomerId is excluded because it is decorated with ScaffoldColumn(false))</p>
<p>If we also want to add an “Edit” link for the customer, then we could add an additional column:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:40529881-3035-4a98-aca8-d2068d2ae5c9" class="wlWriterEditableSmartContent">

<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;">AutoGenerateColumns</span><span style="color: #006600; font-weight:bold;">&#40;</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>extraColumns <span style="color: #006600; font-weight: bold;">=&gt;</span> <span style="color: #006600; font-weight:bold;">&#123;</span>
        extraColumns.<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;Edit Customer&quot;</span>, <span style="color: #cc0000;">&quot;Edit&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> x.<span style="color: #9900cc;">CustomerId</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: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

</div>
<p>The additional column contains a link to the edit page for each customer. Note that while columns are automatically HTML-encoded, the grid will detect that ActionLink returns an instance of MvcHtmlString and therefore won’t encode this particular column.</p>
<p>Additional columns can also be re-ordered. For example, if we wanted our “Edit” link to appear before the Name/DateOfBirth then the <strong>InsertAt </strong>method could be used:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:83f8e90d-f263-4fa5-b0a1-30a607477686" class="wlWriterEditableSmartContent">

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%=</span> Html.<span style="color: #0000FF;">Grid</span><span style="color: #000000;">&#40;</span>Model<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">AutoGenerateColumns</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Columns</span><span style="color: #000000;">&#40;</span>extraColumns <span style="color: #008000;">=&gt;</span> <span style="color: #000000;">&#123;</span>
        extraColumns.<span style="color: #0600FF;">For</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> Html.<span style="color: #0000FF;">ActionLink</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Edit Customer&quot;</span>, <span style="color: #666666;">&quot;Edit&quot;</span>, <span style="color: #008000;">new</span><span style="color: #000000;">&#123;</span> id <span style="color: #008000;">=</span> x.<span style="color: #0000FF;">CustomerId</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>
            .<span style="color: #0000FF;">InsertAt</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span></pre></div></div>

</div>
<p>Note that the InsertAt method is not contained in the latest MvcContrib release – you’ll need to build from source to get this.</p>
<p>I’ve also started to put together some <a href="http://mvccontrib.codeplex.com/wikipage?title=Grid">documentation for the grid</a> on the MvcContrib wiki.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>MvcContrib Grid Part 6 &#8211; Sorting</title>
		<link>http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/</link>
		<comments>http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 13:48:00 +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/2010/03/14/mvccontrib-grid-part-6-sorting/</guid>
		<description><![CDATA[This is part 6 of a series about the MvcContrib Grid. Part 1 – Introduction Part 2 – New Syntax Part 3 – GridModels and GridRenderes Part 4 – Limitations of the WebFormsViewEngine Part 5 – The Action Syntax Part 6 – Sorting Part 7 – Auto-generated columns I recently added single-column sorting support to [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 6 of a series about the MvcContrib Grid.</p>
<ul>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/08/rewriting-the-mvccontrib-grid/">Part 1 – Introduction</a> </li>
<li><a href="http://www.jeremyskinner.co.uk/2009/02/22/rewriting-the-mvccontrib-grid-part-2-new-syntax/">Part 2 – 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 – 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 – 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 – The Action Syntax</a> </li>
<li><a href="http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/">Part 6 – Sorting</a> </li>
<li><a href="http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/">Part 7 – Auto-generated columns</a> </li>
</ul>
<p>I recently added single-column sorting support to the MvcContrib grid. This can be turned on by calling the “Sort” method on a grid declaration and accepting a parameter of type GridSortOptions in your controller actions.</p>
<p>Controller:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:92bcd9ca-e271-4b4b-a97e-1f2912870984" class="wlWriterEditableSmartContent">

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> ActionResult Sorting<span style="color: #000000;">&#40;</span>GridSortOptions sort<span style="color: #000000;">&#41;</span> 
<span style="color: #000000;">&#123;</span>
  ViewData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;sort&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> sort<span style="color: #008000;">;</span>
  var people <span style="color: #008000;">=</span> _peopleFactory.<span style="color: #0000FF;">CreatePeople</span><span style="color: #000000;">&#40;</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><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>sort.<span style="color: #0000FF;">Column</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    people <span style="color: #008000;">=</span> people.<span style="color: #0000FF;">OrderBy</span><span style="color: #000000;">&#40;</span>sort.<span style="color: #0000FF;">Column</span>, sort.<span style="color: #0000FF;">Direction</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>people<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</div>
<p>View:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:f60fcfee-7464-4712-a63a-ed9dcbf3c283" class="wlWriterEditableSmartContent">

<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;">Sort</span><span style="color: #006600; font-weight:bold;">&#40;</span>ViewData<span style="color: #006600; font-weight:bold;">&#91;</span><span style="color: #cc0000;">&quot;sort&quot;</span><span style="color: #006600; font-weight:bold;">&#93;</span> <span style="color: #330066;">as</span> GridSortOptions<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>
    <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>

</div>
<p>By calling the “Sort” method, this tells the grid that sorting should be enabled. This will generate sorting links for each column heading (note that currently sorting is enabled for all columns – there is no way to enable it for individual columns only. I’ll change this in a future release)</p>
<p>These sorting links will add two additional parameters to the querystring: Column and Direction. These can be handled in your controller action by taking as a parameter an object of type GridSortOptions.</p>
<p>Using this object, you can then sort your data using whatever mechanism you choose. Out of the box, MvcContrib provides an overload for the “OrderBy” extension method that takes a property name and a direction and delegates the sorting to IQueryable. However, use of this extension method is entirely optional.</p>
<p>This also works with the grid’s paging support, so if you want your data to be both paged and sorted then you can make a call to AsPagination (or by using a custom IPagination provider) after the call to OrderBy:</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:39a91ae4-7c05-4df8-ae53-cbc4f740b801" class="wlWriterEditableSmartContent">

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> ActionResult SortingAndPaging<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">?</span> page, GridSortOptions sort<span style="color: #000000;">&#41;</span> 
<span style="color: #000000;">&#123;</span>
  ViewData<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;sort&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> sort<span style="color: #008000;">;</span>
  var people <span style="color: #008000;">=</span> _peopleFactory.<span style="color: #0000FF;">CreatePeople</span><span style="color: #000000;">&#40;</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><span style="color: #008000;">!</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #000000;">&#40;</span>sort.<span style="color: #0000FF;">Column</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    people <span style="color: #008000;">=</span> people.<span style="color: #0000FF;">OrderBy</span><span style="color: #000000;">&#40;</span>sort.<span style="color: #0000FF;">Column</span>, sort.<span style="color: #0000FF;">Direction</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
&nbsp;
  people <span style="color: #008000;">=</span> people.<span style="color: #0000FF;">AsPagination</span><span style="color: #000000;">&#40;</span>page <span style="color: #008000;">??</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">10</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">return</span> View<span style="color: #000000;">&#40;</span>people<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

</div>
<p>…and the results will be as expected.</p>
<p>When a column is sorted, the CSS classes of “sort_asc” or “sort_desc” will be applied to the relevant &lt;th&gt; element, so you can style the column headings (for example, by adding up/down arrows).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<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 Part 6 – Sorting Part 7 – Auto-generated columns The rewritten MvcContrib grid now has [...]]]></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>
<li><a href="http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/">Part 6 – Sorting</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/">Part 7 – Auto-generated columns</a>
</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>29</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 Part 6 – Sorting Part 7 – Auto-generated columns I was recently asked a question [...]]]></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>
<li><a href="http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/">Part 6 – Sorting</a> </li>
<li><a href="http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/">Part 7 – Auto-generated columns</a> </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 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:c7dd5ff8-5031-41cd-b4ea-5d1eb497faa4" class="wlWriterEditableSmartContent">

<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>

</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 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:e8bd4236-74a1-4871-bddd-1cfed8b7bb52" class="wlWriterEditableSmartContent">

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%=</span> Html.<span style="color: #0000FF;">Grid</span><span style="color: #000000;">&#40;</span>Model.<span style="color: #0000FF;">People</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Columns</span><span style="color: #000000;">&#40;</span>...<span style="color: #000000;">&#41;</span> <span style="color: #008000;">%&gt;</span></pre></div></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 style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:4318B029-7F5A-4155-A9CF-329C469B73B3:3c1c523b-d80a-4d72-acd0-ce360811e3c5" class="wlWriterEditableSmartContent">

<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>

</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 Part 6 – Sorting Part 7 – Auto-generated columns Internally, the new MvcContrib grid makes [...]]]></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>
<li><a href="http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/">Part 6 – Sorting</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/">Part 7 – Auto-generated columns</a>
</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>21</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 Part 6 – Sorting Part 7 – Auto-generated columns Edit: 28 Feb 09 I [...]]]></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>
<li><a href="http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/">Part 6 – Sorting</a></li>
<li><a href="http://www.jeremyskinner.co.uk/2010/04/27/mvccontrib-grid-part-7-auto-generated-columns/">Part 7 – Auto-generated columns</a>
</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>44</slash:comments>
		</item>
	</channel>
</rss>
