Hanalei, Hawaii 9/2/2010
438 Posts and Counting

ASP.NET MVC: PagedList<T>

Monday, December 10, 2007 -

In some of his demos, ScottGu used a method that we didn't include in the MVCToolkit called "PagedList<T>", which basically pages a Linq query. I'm hoping to include this in the next drop of the Toolkit, but until then, I have the code for you :). Just drop this into a code file on your site, the Extension method will be picked up by any class "using" System.Web.Mvc:

using System;
using System.Collections.Generic;
using System.Linq;

namespace System.Web.Mvc
{
    public interface IPagedList
    {
        int TotalCount
        {
            get;
            set;
        }

        int PageIndex
        {
            get;
            set;
        }

        int PageSize
        {
            get;
            set;
        }

        bool IsPreviousPage
        {
            get;
        }

        bool IsNextPage
        {
            get;
        }     
    }

    public class PagedList<T> : List<T>, IPagedList
    {
        public PagedList(IQueryable<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
        }    
        
        public PagedList(List<T> source, int index, int pageSize)
        {
            this.TotalCount = source.Count();
            this.PageSize = pageSize;
            this.PageIndex = index;
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
        }
    
        public int TotalCount      
        { 
            get; set; 
        }
        
        public int PageIndex       
        { 
            get; set; 
        }
        
        public int PageSize 
        { 
            get; set; 
        }

        public bool IsPreviousPage 
        { 
            get 
            {
                return (PageIndex > 0);
            }
        }
        
        public bool IsNextPage 
        { 
            get
            {
                return (PageIndex * PageSize) <=TotalCount;
            } 
        }        
    }

    public static class Pagination
    {
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize) 
        {
            return new PagedList<T>(source, index, pageSize);
        }

        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
        {
            return new PagedList<T>(source, index, 10);
        }        
    }
}

Related


Gravatar
Troy DeMonbreun - Monday, December 10, 2007 - Thanks!
Gravatar
Vijay S. - Monday, December 10, 2007 - I added a TotalPageCount property to my copy, do u think this is a good idea?
Gravatar
Vijay S. - Monday, December 10, 2007 - public int TotalPageCount { get { return (TotalCount / PageSize) 1; } } Also, when there's only one page of data, isNextPage has a bug it should be public bool IsNextPage { get { return ((PageIndex * PageSize)
Gravatar
Vijay S. - Monday, December 10, 2007 - Actually, this works better return ((PageIndex 1) * PageSize)
Gravatar
Vijay S. - Monday, December 10, 2007 - Sorry, copy n paste didn't work. TotalPageCount should be return (TotalCount / PageSize) 1;
Gravatar
Anastasiosyal - Monday, December 10, 2007 - Cool stuff :) Minest of details but wouldnt you agree that it should be HasPreviousPage and HasNextPage rather than IsPreviousPage and IsNextPage?
Gravatar
mike - Tuesday, December 11, 2007 - Sooo, under what licence is this code released, GPL v3? ;-)
Gravatar
mike - Tuesday, December 11, 2007 - IsNextPage doesn't sound right imho. I would prefer IsFirstPage and IsLastPage.
Gravatar
Jim Zimmerman - Wednesday, December 12, 2007 - Cool stuff! I love these extension methods. One very cool thing about them is if someone really wants HasPreviousPage instead of IsPreviousPage or wants it be IsPP or DoesThisHaveAPreviousPage they can. Some conventions will really help, but for those who want their own, they can call it whatever they want.
Gravatar
Vijay Santhanam - Wednesday, December 12, 2007 - So true Jim, but if this is going to in a framework it definitely needs some descriptive naming. I found this class so useful on my first MVC app. I'm so glad to be done with Web.UI.Controls
Gravatar
Wöchentliche Rundablage: ASP.NET MVC, ADO.NET Dataservices ("Astoria"), ASP.NET, LINQ, WPF | Code-Inside Blog - Monday, December 17, 2007 - [...] ASP.NET MVC: PagedList<T> Source Code [...]
Gravatar
Francois Tanguay - Monday, December 17, 2007 - Just my 2 cents:

IPagedList : IList ?

PagedList Ctor should accept IEnumerable, hence having only 1 ctor.

So should extension methods.

AddRange(.... .ToList()); => ToList() isn't required. AddRange accepts any IEnumerable
Gravatar
December 16th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, VS, .NET, IIS7, WPF « .NET Framework tips - Thursday, December 20, 2007 - [...] PagedList<T> Support: Rob Conery posts a sample implementation of a pageable List<T> implementation that I showed in my original ASP.NET MVC demo at the Alt.net conference. [...]
Gravatar
SubsoniC - Saturday, March 08, 2008 - Why do this with LINQ when SubSonic already delivers? I'm not sure why there are two overlapping DALs at Microsoft, come to think of it...
Gravatar
Turhal Temizer - Monday, March 24, 2008 - Thanks...
Gravatar
acewomake - Wednesday, April 15, 2009 - emm.. good one )
Gravatar
Marko - Wednesday, April 22, 2009 - I don't like this approach. I tried to use it and what I have to do to make it works is rewriting all the places where I am using plain List<>.
Gravatar
PagedList Implementation in Visual Basic 9 - Agus Suhanto - Thursday, April 23, 2009 - [...] Conery first created a post about PagedList. A PagedList is like a sliced list, a predefined subset of whole items in a collection.The purpose [...]
Gravatar
A Tale of Valentica » Blog Archive » Pagination Class for ASP.NET MVC - Thursday, May 28, 2009 - [...] was looking for a pagination class to use in Asp.Net Mvc. I found ASP.NET MVC: PagedList<T> and ASP.NET MVC - Pagination View User Control. But I felt for an easier and customizable [...]