Simple MVC 2 Pager

I wrote a simple pager AjaxHelper extension.  You can convert this to an HtmlExtension method by just removing the AjaxOptions and swapping out the AjaxHelper with HtmlHelper.  This assumes that your parameter for the page name is "Page".

					   1:  
					public
					static
					string Pager(this AjaxHelper ajaxHelper, int page,   

int pageSize, int count, int pages, string actionName, string controllerName,
object routData, AjaxOptions ajaxOptions)

					   2:        {

					   3:            StringBuilder sb = new StringBuilder();

					   4:  
					int pageCount = count / pageSize + 1;

					   5:  
					int firstPage = page / pages \* pages;

					   6:  
					int lastPage = firstPage + pages;

					   7:  
					if (firstPage == 0)

					   8:            {

					   9:                firstPage = 1;

					  10:            }

					  11:  
					if (firstPage > 1)

					  12:            {

					  13:                firstPage--;

					  14:            }

					  15:  
					if (lastPage > pageCount)

					  16:            {

					  17:                lastPage = pageCount;

					  18:            }

					  19:   

					  20:  
					if (firstPage > 1)

					  21:            {

					  22:                RouteValueDictionary routeValueDictionary = new RouteValueDictionary(routData);

					  23:                routeValueDictionary\["Page"\] = 1;

					  24:                sb.Append(ajaxHelper.ActionLink("<<", actionName, controllerName, routeValueDictionary, ajaxOptions).ToHtmlString());

					  25:                sb.Append("&nbsp;");

					  26:            }

					  27:  
					for (int i = firstPage; i <= lastPage; i++)

					  28:            {

					  29:  
					if (sb.Length > 0)

					  30:                {

					  31:                    sb.Append("&nbsp;");

					  32:                }

					  33:  
					if (i == page)

					  34:                {

					  35:                    sb.AppendFormat("{0}", i);

					  36:                }

					  37:  
					else
			

					  38:                {

					  39:  
					string linkText = Convert.ToString(i);

					  40:  
					if ((i == firstPage && i > 1) || (i == lastPage && i < pageCount))

					  41:                    {

					  42:                        linkText = "...";

					  43:                    }

					  44:                    RouteValueDictionary routeValueDictionary = new RouteValueDictionary(routData);

					  45:                    routeValueDictionary\["Page"\] = i;

					  46:                    sb.Append(ajaxHelper.ActionLink(linkText, actionName, controllerName, routeValueDictionary, ajaxOptions).ToHtmlString());

					  47:                }

					  48:            }

					  49:  
					if (lastPage < pageCount)

					  50:            {

					  51:                RouteValueDictionary routeValueDictionary = new RouteValueDictionary(routData);

					  52:                routeValueDictionary\["Page"\] = pageCount;

					  53:                sb.Append("&nbsp;");

					  54:                sb.Append(ajaxHelper.ActionLink(">>", actionName, controllerName, routeValueDictionary, ajaxOptions).ToHtmlString());

					  55:            }

					  56:  
					return sb.ToString();

					  57:        }
This article is part of the GWB Archives. Original Author: Doug Lampe

New on Geeks with Blogs