ASP.NET MVC "Pager" HTML Helper

So I am building my own custom blog using the ASP.NET MVC Framework, and I have been working on the paging aspect.  I will be going into more detail how i achieved this but for now, I am publishing a HtmlHelper I have made which has helped me, believe it or not lol, in the paging.  The way I have designed it also is kind of decoupled, so i see no reason for its integration into many other applications.

 

My implmentation of the HtmlHelper is here:

 

<%= Html.Pager((pc.StartingIndex / pc.PageSize) ,pc.PageSize,pc.TotalRecords,"/Home/Page") %>

 

Pager

And the code which makes it is here.  The major thing is calculating the seed of the iteration based on the current page, after that you are laughing!

 

    public static string Pager(this HtmlHelper helper, int currentPage, int currentPageSize, int totalRecords, string urlPrefix)
    {
        StringBuilder sb1 = new StringBuilder();

        int seed = currentPage % currentPageSize == 0 ? currentPage : currentPage - (currentPage % currentPageSize);

        if(currentPage > 0)
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Previous</a>", urlPrefix, currentPage));

        if(currentPage - currentPageSize >= 0)
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage - currentPageSize) + 1));

        for (int i = seed; i < Math.Round((totalRecords / 10) + 0.5) && i < seed + currentPageSize; i++)
        {
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">{1}</a>", urlPrefix, i + 1));
        }

        if (currentPage + currentPageSize <= (Math.Round((totalRecords / 10) + 0.5) - 1))
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">...</a>", urlPrefix, (currentPage + currentPageSize) + 1));

        if (currentPage < (Math.Round((totalRecords / 10) + 0.5) - 1))
            sb1.AppendLine(String.Format("<a href=\"{0}/{1}\">Next</a>", urlPrefix, currentPage + 2));

        return sb1.ToString();
    }

Hope this helps some people,

 

Cheers,

 

Andrew :-)

Published Tuesday, July 01, 2008 4:26 PM by REA_ANDREW

Comments

# ASP.NET MVC Archived Blog Posts, Page 1

Tuesday, July 01, 2008 12:33 PM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# re: ASP.NET MVC "Pager" HTML Helper

Wednesday, July 02, 2008 12:28 AM by Michael Hart

Have you seen the MVCContrib project? It's been around for a while and they have an implementation of this concept (as well as many more)

www.codeplex.com/MVCContrib

# re: ASP.NET MVC "Pager" HTML Helper

Wednesday, July 02, 2008 3:54 AM by REA_ANDREW

Michael : Just browsing that now.  What an ace find!! Cheers :-)

# re: ASP.NET MVC "Pager" HTML Helper

Wednesday, July 02, 2008 1:01 PM by Meisinger

yeah... the MvcContrib does have this concept but... what is the fun in that?

what better way to learn and have fun then to do it yourself?

as far as the code is concerned...

great job and an interesting solution

i would only change a few things

create a local variable for the "Math.Round" logic

if you ever decide to show 5 pages versus 10 then you have one place to change the code vs. three

var totalPages = Math.Round((totalRecords / 10) + 0.5);

also consider using the HtmlHelper ActionLink method

this will construct the url and the link for you without you having to write it out manually

this is also helpful if your routes ever change... you wouldn't have to change anything in your code

of course this would require you to then pass in the controller and the action as parameters to the method

-- assume [controller] and [action] are string parameters

sb1.Append(helper.ActionLink("Previous", controller, action));

then to top it off you could really add some neat functionaltiy by making a generic version of you method that takes T as a controller

then instead of passing in the [controller] and [action] as string parameters you could pass in an Expression of Action of T (or Expression<Action<T>>

with that then you could call

Html.Paging<HomeController>((pc.StartingIndex / pc.PageSize), pc.PageSize, pc.TotalRecords, c => c.Page())

man... i am getting excited just thinking about it

# re: ASP.NET MVC "Pager" HTML Helper

Wednesday, July 02, 2008 1:09 PM by REA_ANDREW

Cheers for that Meisinger the generic method sounds like a must.  Cheers again :-)

# re: ASP.NET MVC "Pager" HTML Helper

Wednesday, July 22, 2009 12:28 PM by RichB

StringBuilder has an AppendFormat() method.

# re: ASP.NET MVC "Pager" HTML Helper

Wednesday, July 22, 2009 2:16 PM by Mark Buckley

I've just found this page today and I'm attempting to implement this pager. I'm also using the PagedList found here www.squaredroot.com/.../PagedList-Strikes-Back I can get the first page to display fine however any link I click on does nothing. It will not change pages. It appears that the controller is not recognizing the 'currentPage' parameter that is being passed in the link...

My controller code look like this:

public ActionResult Index(int? currentPage)

       {

           int currentPageIndex = currentPage.HasValue ? currentPage.Value : 0;

           return View(_db.Addresses.ToPagedList(currentPageIndex, defaultPageSize));

       }

Any idea why this is'nt working? The currentPageIndex ALWAYS equals 0 no matter what link I click on...

Appreciate any thoughts..

-MARK-

# re: ASP.NET MVC "Pager" HTML Helper

Tuesday, July 27, 2010 9:36 PM by amalatsliit

hi could u pls tell me what is 'pc.PageSize' means just confused..what sort value that can be...

# re: ASP.NET MVC "Pager" HTML Helper

Wednesday, August 11, 2010 6:45 AM by Francesco Biacca

@amalatsliit: i think it's row's number per page

# re: ASP.NET MVC "Pager" HTML Helper

Tuesday, April 05, 2011 8:30 AM by weblogs.asp.net

Asp net mvc quot pager quot html helper.. Amazing :)

# re: ASP.NET MVC "Pager" HTML Helper

Saturday, April 30, 2011 7:13 PM by weblogs.asp.net

Asp net mvc quot pager quot html helper.. Corking :)

# re: ASP.NET MVC "Pager" HTML Helper

Friday, May 13, 2011 4:57 AM by Ralemazoto

Here another method to render a pager links like Gmail.

public static MvcHtmlString Pager(this HtmlHelper helper, PagerInfo pagerInfo)

       {

           StringBuilder sb1 = new StringBuilder();

           int seed = pagerInfo.CurrentPage % pagerInfo.ItemsPerPage == 0 ? pagerInfo.CurrentPage : pagerInfo.CurrentPage - (pagerInfo.CurrentPage % pagerInfo.ItemsPerPage);

           if (pagerInfo.CurrentPage == 1)

               sb1.AppendLine(String.Format("First"));

           else if (pagerInfo.CurrentPage > 1)

               sb1.AppendLine(String.Format("<a href=\"1\">First</a>"));

           if (pagerInfo.CurrentPage == 1)

               sb1.AppendLine(String.Format("Previous"));

           else if (pagerInfo.CurrentPage > 1)

               sb1.AppendLine(String.Format("<a href=\"{0}\">Previous</a>", pagerInfo.CurrentPage-1));

           sb1.Append(String.Format("<span>{0} à {1} sur {2}</span>", (pagerInfo.CurrentPage * pagerInfo.ItemsPerPage) - pagerInfo.ItemsPerPage + 1, (pagerInfo.CurrentPage * pagerInfo.ItemsPerPage), pagerInfo.TotalItems));

           if (pagerInfo.CurrentPage < pagerInfo.TotalPages)

               sb1.AppendLine(String.Format("<a href=\"{0}\">Next</a>", pagerInfo.CurrentPage + 1));

           else if (pagerInfo.CurrentPage == pagerInfo.TotalPages)

               sb1.AppendLine(String.Format("Next"));

           if (pagerInfo.CurrentPage > 0)

               sb1.AppendLine(String.Format("<a href=\"{0}\">Last</a>", pagerInfo.TotalPages));

           else if (pagerInfo.CurrentPage == pagerInfo.TotalPages)

               sb1.AppendLine(String.Format("Last"));

           return MvcHtmlString.Create(sb1.ToString());

       }

   }

# Share Link &laquo; Blog Tutorial Del

Monday, May 30, 2011 2:50 AM by Share Link « Blog Tutorial Del

Pingback from  Share Link &laquo; Blog Tutorial Del

# re: ASP.NET MVC "Pager" HTML Helper

Monday, June 13, 2011 9:40 PM by weblogs.asp.net

Asp net mvc quot pager quot html helper.. He-he-he :)

# Share Link &laquo; Blog Tutorial Del

Wednesday, June 29, 2011 10:29 PM by Share Link « Blog Tutorial Del

Pingback from  Share Link &laquo;  Blog Tutorial Del

Leave a Comment

(required) 
(required) 
(optional)
(required)