It’s now possible to add the MVC pager to your own project with NuGet.

image

Or from Visual Studio with ‘Manage NuGet Packages…’ and then search for ‘MvcPaging’.

After installing, you can immediately use the Pager HtmlHelper in your views:

image

et voilà!

image

We also have include the IPagedList for your convenience:

image

For more info, check out the original blogpost that started it or the github project page.

Enjoy!

NuGet package for MVC pager
Tagged on:         

11 thoughts on “NuGet package for MVC pager

  • May 15, 2011 at 6:14 pm
    Permalink

    Hi Martijn,

    I am having a problem with the pager when I define custom routes. For instance in your MvcPaging.Demo project I add in Global.asax two routes before the default route:

    routes.MapRoute(“Test1”, “Test1”,
    new { controller = “Home”, action = “About” },
    new[] { “MvcPaging.Demo.Controllers” });

    routes.MapRoute(“Test2”, “Test2”,
    new { controller = “Paging”, action = “Index” },
    new[] { “MvcPaging.Demo.Controllers” });

    // default route comes here

    When I click then on “Simple Paging” on your start page I get redirected to “/Test2” which is fine because the action link points to controller “Paging” and action “Index”.

    However the page links do not point to “/Test2?page=n” but to “/Test1?page=n” (so I get redirected to the About page with my route configuration above when I click on a page link).

    I have tracked down the issue to “GetVirtualPathForArea” in “GeneratePageLink()” which returns those wrong links (it seems to return simply the first entry in the routing table which is wrong one in this case).

    Do you have an idea how to fix this?

    Thanks for feedback and thanks for your great work!

  • May 15, 2011 at 10:03 pm
    Permalink

    OK, thanks, this helps. I’ve noticed that you also can add the controller as constraint instead of the action (which is even necessary if both actions have the same name but only a different controller):

    routes.MapRoute(“Test1”, “Test1″,
    new { controller = “Home”, action = “Index” },
    new { controller = “Home” }
    new[] { “MvcPaging.Demo.Controllers” });

    I am wondering if one could use “ActionLink” instead of “GetVirtualPathForArea”. I am also using this sorting helper here (http://www.codecapers.com/post/sorting-data-in-aspnet-mvc.aspx) and it delegates a part of the link generation to the ActionLink helper. This sorting helper doesn’t have the described problem (works also with areas) and the links seem to be always correct, no matter how I define my routes. But my MVC knowledge isn’t advanced enough to see if ActionLink could also be an option for the Paging helper or not.

  • May 15, 2011 at 10:20 pm
    Permalink

    If I remember well, I tried it first with ActionLink, but at the time (MVC pre-V1) there was some blocking issue, therefore I took the GetVirtualPath(ForArea) route.
    It might very well be possible that ActionLink is the better option nowadays.

  • July 5, 2011 at 1:00 pm
    Permalink

    How to integrate the paging with styles and CSS?

  • July 5, 2011 at 2:27 pm
    Permalink

    @shijuvar: wrap the pager in a div with a specific class (for example ‘.pager’) or id. Then you can style it easily:

    .pager
    {
    }

    .pager .disabled
    {
    }

    .pager .current
    {
    }

    .pager span, .pager a
    {
    }

    etc…

    The pager outputs 2 specific css classes: ‘current’ for the current page and ‘disabled’ for disabled hyperlinks.

  • October 25, 2011 at 5:22 pm
    Permalink

    Hey I love your visual studio theme, what is it?
    (also the pager is pretty cool)

    Cheers

    Paul

  • December 3, 2011 at 5:43 am
    Permalink

    Hello Martijn:

    I am not sure this pager addresses the real problem. Say, you have 5 million records. Your search returns 2 million. Now you wish to display the first 20 records and then the next 20 and so on.

    The problem I see is that it retrieves the full 2 million records and then ‘takes’ 20 and throws away the rest.

    Your thoughts are appreciated.

    Jan

  • December 3, 2011 at 11:39 am
    Permalink

    @Jan
    The Pager Helper itself doesn’t do anything with data. You just tell it how large your pagesize is, which page we’re on and the total number of items.

    The PagedList that comes with the Pager can be created from an IQueryable (returned from NHibernate or EF) which means that actual paging is executed via the database query.
    Alternatively, you could even use a PagedList, set its properties and perform all the paging logic yourself.

  • Pingback:Martijn Boland » Paging with ASP.NET MVC

Leave a Reply

Your email address will not be published. Required fields are marked *