Over a million developers have joined DZone.

Simple Pager for ASP.NET MVC


I needed simple pager for one of my ASP.NET MVC projects. I mean really simple pager – no new types my code should be aware of, no any other chemistry I don’t really need in early stages. As ASP.NET MVC has nothing to offer I wrote my own *really simple* pager implementation. Here is what I did.

NB! The code here is very fresh and I take it as a fast prototype solution. I provided it to you to give you simple pager you can use without knowing about too much details. You can quickly and easily use it in your ASP.NET MVC applications and application prototypes. Feel free to modify my code if you like.

To get pager built more optimally I created PagerBuilder class. It takes some parameters and based on them it generates pager mark-up when ToString() method is called (just like StringBuilder returns string with its contents when ToString() is called). This is my PagerBuilder.

public class PagerBuilder
private class PagerLink
public string Title { get; set; }
public int PageNo { get; set; }
public string Class { get; set; }

private readonly string _urlTemplate;
private readonly List<PagerLink> _pagerLinks = new List<PagerLink>();

public PagerBuilder(string urlTemplate)
_urlTemplate = urlTemplate;

public string PagerClass { get; set; }

public void AddPage(string title, int pageNo)
AddPage(title, pageNo, string.Empty);

public void AddPage(string title, int pageNo, string itemClass)
var link = new PagerLink
PageNo = pageNo,
Title = title,
Class = itemClass

public override string ToString()
var builder = new StringBuilder();

if (!string.IsNullOrEmpty(PagerClass))
builder.Append(" class=\"");

foreach (var link in _pagerLinks)
builder.Append("<a href=\"");
builder.AppendFormat(_urlTemplate, link.PageNo);

if (!string.IsNullOrEmpty(link.Class))
builder.Append(" class=\"");



return builder.ToString();

To use pager in views I created extension method called SimplePager for HtmlHelper class. Here is my extension method.

public static string  SimplePager(this HtmlHelper helper,
int currentPage, int pageCount, string urlTemplate,
string pagerClass)
if (currentPage < 0) currentPage = 1;
if (pageCount < 0) pageCount = 0;

var pager = new PagerBuilder(urlTemplate);
pager.PagerClass = pagerClass;

if (currentPage > 1)
pager.AddPage("<<", 1);
pager.AddPage("<", 1);

var start = Math.Max(currentPage - 2, 1);
var end = Math.Min(pageCount, currentPage + 2);

for (var i = start; i <= end; i++)
if (i == currentPage)
pager.AddPage(i.ToString(), i, "current");
pager.AddPage(i.ToString(), i);

if (currentPage < pageCount)
pager.AddPage(">", currentPage + 1);
pager.AddPage(">>", pageCount);

return pager.ToString();

It needs current page index and page count to generate pager. You must also provide pager class name and URL-template.

<%= Html.SimplePager(Model.CurrentPage, Model.PageCount,
"/contacts/page/{0}", "pager")

As you can see from example above you can use your model to provide pager with current page and page count. In my case it is okay because repository behind this view returns me paged results so I have no problems with getting current page index and page count.

Currently my pager looks like this after very short playing with styles. Not very nice but it works and some design dude who helps me out can easily make it look very nice.

My ASP.NET MVC  pages

Feel free to use and modify my code. If you have questions or suggestions then please drop  your comment here. Happy paging! :)



Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}