5

I have implemented a paging html helper (adapted from steven sanderson's book). This is the current code:

public static string PageLinks(this HtmlHelper html, int currentPage, int totalPages, Func pageUrl) { StringBuilder result = new StringBuilder();

        for (int i = 1; i <= totalPages; i++)
        {
            TagBuilder tag = new TagBuilder("a");
            tag.MergeAttribute("href", pageUrl(i));
            tag.InnerHtml = i.ToString();
            if (i == currentPage)
                tag.AddCssClass("selectedPage");
            result.AppendLine(tag.ToString());
        }

        return result.ToString();
    }

This produces a bunch of links to each page of my items. If there are many pages this can be a bit overwhelming. I am looking for a similar implementation which produces something less overwhelming like this:

where 6 is the current page. I am sure someone must have implemented something similar ... before I have to re-implement the wheel.

Thanks.

Christian

Marc Climent
  • 9,434
  • 2
  • 50
  • 55
cs0815
  • 16,751
  • 45
  • 136
  • 299
  • sorry I only got around to look at this today - will accept your answer asap. both answers seem to be good so not sure what to do. – cs0815 May 26 '10 at 10:32

3 Answers3

3

There's a pager helper in MVCContrib.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Looks great thanks. I used to use the jqgrid but try to avoid unobtrusive javascript as much as possible at the moment. Are you, by any chance, aware of a 'mvc grid' like this which also allows sorting and filtering? thanks. – cs0815 May 21 '10 at 07:19
  • MVCContrib Grid supports sorting: http://www.jeremyskinner.co.uk/2010/03/14/mvccontrib-grid-part-6-sorting/ – Darin Dimitrov May 21 '10 at 07:23
  • I have problems with this bit: <%= Html.Pager((IPagination)Model) %> when I compile it complains that it cannot find the html helper pager. is this just an import issue? I am using: <%@ Import Namespace="MvcContrib.UI.Grid" %> <%@ Import Namespace="MvcContrib.Pagination" %> – cs0815 May 26 '10 at 12:56
  • ok sorted. had to include this: <%@ Import Namespace="MvcContrib.UI.Grid" %> <%@ Import Namespace="MvcContrib.Pagination" %> <%@ Import Namespace="MvcContrib.UI.Pager" %> – cs0815 May 26 '10 at 13:39
0

I'm using this pager (works with MVC2 too): http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/

I found it really good.

Palantir
  • 23,820
  • 10
  • 76
  • 86
-1

You can see this link for more detail MVC HTML Paging Helper with search and sort

I have implemented it with search and sort and explained it in detail how we can extend and register and use with any page.

Ali Adravi
  • 21,707
  • 9
  • 87
  • 85