<?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>Martijn Boland &#187; Paging</title>
	<atom:link href="http://blogs.taiga.nl/martijn/tag/paging/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.taiga.nl/martijn</link>
	<description>New adventures in .NET</description>
	<lastBuildDate>Tue, 27 Apr 2010 20:51:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Paging demo for ASP.NET MVC 2</title>
		<link>http://blogs.taiga.nl/martijn/2010/01/27/paging-demo-for-asp-net-mvc-2/</link>
		<comments>http://blogs.taiga.nl/martijn/2010/01/27/paging-demo-for-asp-net-mvc-2/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 18:27:58 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Area]]></category>
		<category><![CDATA[Paging]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/2010/01/27/paging-demo-for-asp-net-mvc-2/</guid>
		<description><![CDATA[During work, I discovered that the pager that I created a while ago didn’t work properly when using ASP.NET MVC2 area’s. The links that were generated didn’t count for the current area that the controller and views were in, resulting in wrong urls. Luckily the ASP.NET MVC team also ran into this issue and created [...]]]></description>
			<content:encoded><![CDATA[<p>During work, I discovered that the <a href="http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/" target="_blank">pager that I created a while ago</a> didn’t work properly when using ASP.NET MVC2 area’s. The links that were generated didn’t count for the current area that the controller and views were in, resulting in wrong urls. Luckily the ASP.NET MVC team also ran into this issue and created an GetVirtualPathForArea() extension method on RouteCollection. Calling this one, instead of GetVirtualPath() made things work properly in area’s.</p>
<p>You can get the code for ASP.NET MVC 2 at <a title="http://github.com/martijnboland/MvcPaging/tree/mvc2" href="http://github.com/martijnboland/MvcPaging/tree/mvc2">http://github.com/martijnboland/MvcPaging/tree/mvc2</a> (mvc2 branch of the MvcPaging repository).</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2010/01/27/paging-demo-for-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Paging demo upgraded to ASP.NET MVC RC</title>
		<link>http://blogs.taiga.nl/martijn/2009/02/04/paging-demo-upgraded-to-aspnet-mvc-rc/</link>
		<comments>http://blogs.taiga.nl/martijn/2009/02/04/paging-demo-upgraded-to-aspnet-mvc-rc/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 11:04:53 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Paging]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/2009/02/04/paging-demo-upgraded-to-aspnet-mvc-rc/</guid>
		<description><![CDATA[The sample project that belongs to the Paging with ASP.NET MVC post is upgraded to ASP.NET MVC RC. Get it from here.]]></description>
			<content:encoded><![CDATA[<p>The sample project that belongs to <a href="http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/" target="_blank">the Paging with ASP.NET MVC post</a> is upgraded to ASP.NET MVC RC. <a href="http://blogs.taiga.nl/martijn/wp-content/uploads/2008/08/mvcpaging-rc1.zip">Get it from here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2009/02/04/paging-demo-upgraded-to-aspnet-mvc-rc/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Paging demo upgraded to ASP.NET MVC Beta</title>
		<link>http://blogs.taiga.nl/martijn/2009/01/16/paging-demo-upgraded-to-aspnet-mvc-beta/</link>
		<comments>http://blogs.taiga.nl/martijn/2009/01/16/paging-demo-upgraded-to-aspnet-mvc-beta/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 11:48:10 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Paging]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/archive/2009/01/16/paging-demo-upgraded-to-asp.net-mvc-beta.aspx</guid>
		<description><![CDATA[The post about paging in ASP.NET MVC still gets many hits, but the demo was created with ASP.NET MVC preview 5, so therefore I upgraded the demo to ASP.NET MVC Beta. I also removed a little bug that occurred when paging an already filtered list. You can download the updated demo here.]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.taiga.nl/martijn/2008/08/27/paging-with-asp.net-mvc">post about paging in ASP.NET MVC</a> still gets many hits, but the demo was created with ASP.NET MVC preview 5, so therefore I upgraded the demo to ASP.NET MVC Beta. I also removed a little bug that occurred when paging an already filtered list.</p>
<p><a href="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/mvcpaging-mvc-beta.zip" target="_blank">You can download the updated demo here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2009/01/16/paging-demo-upgraded-to-aspnet-mvc-beta/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Paging with ASP.NET MVC</title>
		<link>http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/</link>
		<comments>http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 15:50:23 +0000</pubDate>
		<dc:creator>martijn</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Paging]]></category>

		<guid isPermaLink="false">http://blogs.taiga.nl/martijn/archive/2008/08/27/paging-with-asp.net-mvc.aspx</guid>
		<description><![CDATA[Updated 2009-02-04: Upgraded source and demo to ASP.NET MVC RC1 Updated 2009-01-16: Upgraded source and demo to ASP.NET MVC Beta Updated 2008-09-09: Source and Demo now use ASP.NET MVC preview 5 On my latest gig, we used ASP.NET MVC and it was a very pleasant experience! Very clean code, clean html output and the productivity [...]]]></description>
			<content:encoded><![CDATA[<p><em>Updated 2009-02-04: Upgraded source and demo to ASP.NET MVC RC1</em></p>
<p><em>Updated 2009-01-16: Upgraded source and demo to ASP.NET MVC Beta      <br /></em></p>
<p><em>Updated 2008-09-09: Source and Demo now use ASP.NET MVC preview 5</em></p>
<p>On my latest gig, we used <a href="http://asp.net/mvc" target="_blank">ASP.NET MVC</a> and it was a very pleasant experience! Very clean code, clean html output and the productivity was great (which is not very common when using a technology for the first time).</p>
<p>Of course, since ASP.NET MVC isn&#8217;t finished yet, it leaves something to desire. At one time we needed paging and there isn&#8217;t an out-of-the-box solution for it (yet). Some googling revealed that others had already addressed this, but to be honest, I wasn&#8217;t entirely pleased with the solutions, so I grabbed the best parts and build a generic solution out of it.</p>
<p><a href="#download">I&#8217;m in a hurry, take me to the download immediately</a></p>
<h3>IPagedList&lt;T&gt;</h3>
<p>First, there was the PagedList&lt;T&gt; that Scott Guthrie already used in one of the earliest MVC demo&#8217;s and that later appeared in improved incarnations from <a href="http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/" target="_blank">Rob Conery</a> and&#160; <a href="http://www.squaredroot.com/post/2008/07/08/PagedList-Strikes-Back.aspx" target="_blank">Troy Goode</a>. This was a nice starting point, especially because we also used LINQ To SQL as our data access layer. To provide an extra extension point, I created an IPagedList&lt;T&gt; interface instead of using the PagedList&lt;T&gt; directly.</p>
<pre class="brush: csharp;">public interface IPagedList&lt;T&gt; : IList&lt;T&gt;
{
    int PageCount { get; }
    int TotalItemCount { get; }
    int PageIndex { get; }
    int PageNumber { get; }
    int PageSize { get; }
    bool HasPreviousPage { get; }
    bool HasNextPage { get; }
    bool IsFirstPage { get; }
    bool IsLastPage { get; }
}</pre>
<h3>Html.Pager()</h3>
<p>After choosing the IPagedList&lt;T&gt; implementation I started thinking about how I could create an HtmlHelper extension method that renders the page links. First, I made the mistake to couple the helper to the IPagedList&lt;T&gt; and it took me a little while to realize that a paging HTML helper doesn&#8217;t need anything to know about the underlying data source. It&#8217;s sole purpose is to generate a list of hyperlinks for pages and the only thing we need to know is the total amount of items, the page size and the current page. This is what the most basic HtmlHelper extension looks like:</p>
<pre class="brush: csharp;">public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount)</pre>
<p>We&#8217;re assuming that the page links point to the the current controller and current action. Alternatively, it&#8217;s possible to supply a different action:</p>
<pre class="brush: csharp;">public static string Pager(this HtmlHelper htmlHelper, int pageSize, int currentPage, int totalItemCount, string actionName)</pre>
<p>Combining the Pager html helper and IPagedList&lt;T&gt; in a view page (that inherits from ViewPage&lt;IPagedList&lt;Product&gt;&gt;):</p>
<pre class="brush: xml;">&lt;table class=&quot;grid&quot;&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Product name&lt;/th&gt;
            &lt;th&gt;Category&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
        &lt;% foreach (var product in Model) { %&gt;
            &lt;tr&gt;
                &lt;td&gt;&lt;%= product.Name %&gt;&lt;/td&gt;
                &lt;td&gt;&lt;%= product.Category %&gt;&lt;/td&gt;
            &lt;/tr&gt;
        &lt;% } %&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;div class=&quot;pager&quot;&gt;
    &lt;%= Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount) %&gt;
&lt;/div&gt;</pre>
<p>results in this:</p>
<p><a href="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/PagingwithASP.NETMVC_D952/aspnetmvc-pager_2.png"><img style="border-right-width: 0pt; border-top-width: 0pt; border-bottom-width: 0pt; border-left-width: 0pt" border="0" alt="aspnetmvc-pager" src="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/PagingwithASP.NETMVC_D952/aspnetmvc-pager_thumb.png" width="721" height="542" /></a></p>
<p>You probably recognize the digg-style appearance of the pager. Thanks to the ASP.NET MVC sample application <a href="http://www.codeplex.com/Kigg" target="_blank">KIGG</a> for inspiration <img src='http://blogs.taiga.nl/martijn/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>Internally, the pager uses RouteTable.Routes.GetVirtualPath() to render the url&#8217;s so the page url&#8217;s can be configured via routing to create nice looking url&#8217;s like for example &#8216;/Categories/Shoes/Page/1&#8242; instead of &#8216;/Paging/ViewByCategory?name=Shoes&amp;page=1&#8242;.</p>
<h3>Advanced scenarios</h3>
<p>Many scenarios where you want to use paging also use filtering. It&#8217;s possible to pass extra parameters to the Pager helper via a RouteValueDictionary or an anonymous type. This adds these parameters to the page links that are generated:</p>
<pre class="brush: xml;">&lt;div class=&quot;pager&quot;&gt;
    &lt;%= Html.Pager(ViewData.Model.PageSize, ViewData.Model.PageNumber, ViewData.Model.TotalItemCount, new { categoryname = ViewData[&quot;CategoryDisplayName&quot;] } )%&gt;
&lt;/div&gt;</pre>
<p><a href="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/PagingwithASP.NETMVC_D952/aspnetmvc-pager-2_2.png"><img style="border-right-width: 0pt; border-top-width: 0pt; border-bottom-width: 0pt; border-left-width: 0pt" border="0" alt="aspnetmvc-pager-2" src="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/WindowsLiveWriter/PagingwithASP.NETMVC_D952/aspnetmvc-pager-2_thumb.png" width="721" height="542" /></a></p>
<p>That&#8217;s it. A complete solution with demo project can be downloaded below. Feel free to leave any comments or remarks. One of the things I&#8217;m not happy about is that I failed to properly unit test the pager, but that&#8217;s for a different post.</p>
<h3><a name="download">Download</a></h3>
<p><a href="http://blogs.taiga.nl/martijn/wp-content/uploads/subtext/mvcpaging-mvc-beta.zip" target="_blank"></a><a href="http://blogs.taiga.nl/martijn/wp-content/uploads/2008/08/mvcpaging-rc1.zip">Download the paging sample solution, incl. the demo MVC web project</a></p>
<p>Check also the AJAX version of the pager written by Daniel Iglesias: <a title="http://danieliglesiaseng.blogspot.com/2010/01/ajax-paging-with-aspnet-mvc.html" href="http://danieliglesiaseng.blogspot.com/2010/01/ajax-paging-with-aspnet-mvc.html">http://danieliglesiaseng.blogspot.com/2010/01/ajax-paging-with-aspnet-mvc.html</a>.</p>
<p></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
