ASP.NET MVC: PagedList
http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/
![]() using System;
using System;
![]() using System.Collections.Generic;
using System.Collections.Generic;
![]() using System.Linq;
using System.Linq;
![]()
![]() namespace System.Web.Mvc
namespace System.Web.Mvc
![]() {
{
![]() public interface IPagedList
    public interface IPagedList
![]() {
    {
![]() int TotalCount
        int TotalCount
![]() {
        {
![]() get;
            get;
![]() set;
            set;
![]() }
        }
![]()
![]() int PageIndex
        int PageIndex
![]() {
        {
![]() get;
            get;
![]() set;
            set;
![]() }
        }
![]()
![]() int PageSize
        int PageSize
![]() {
        {
![]() get;
            get;
![]() set;
            set;
![]() }
        }
![]()
![]() bool IsPreviousPage
        bool IsPreviousPage
![]() {
        {
![]() get;
            get;
![]() }
        }
![]()
![]() bool IsNextPage
        bool IsNextPage
![]() {
        {
![]() get;
            get;
![]() }
        }
![]() }
    }
![]()
![]() public class PagedList<T> : List<T>, IPagedList
    public class PagedList<T> : List<T>, IPagedList
![]() {
    {
![]() public PagedList(IQueryable<T> source, int index, int pageSize)
        public PagedList(IQueryable<T> source, int index, int pageSize)
![]() {
        {
![]() this.TotalCount = source.Count();
            this.TotalCount = source.Count();
![]() this.PageSize = pageSize;
            this.PageSize = pageSize;
![]() this.PageIndex = index;
            this.PageIndex = index;
![]() this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
![]() }
        }    
![]()
![]() public PagedList(List<T> source, int index, int pageSize)
        public PagedList(List<T> source, int index, int pageSize)
![]() {
        {
![]() this.TotalCount = source.Count();
            this.TotalCount = source.Count();
![]() this.PageSize = pageSize;
            this.PageSize = pageSize;
![]() this.PageIndex = index;
            this.PageIndex = index;
![]() this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
![]() }
        }
![]()
![]() public int TotalCount
        public int TotalCount
![]() {
        {
![]() get; set;
            get; set;
![]() }
        }
![]()
![]() public int PageIndex
        public int PageIndex
![]() {
        {
![]() get; set;
            get; set;
![]() }
        }
![]()
![]() public int PageSize
        public int PageSize
![]() {
        {
![]() get; set;
            get; set;
![]() }
        }
![]()
![]() public bool IsPreviousPage
        public bool IsPreviousPage
![]() {
        {
![]() get
            get
![]() {
            {
![]() return (PageIndex > 0);
                return (PageIndex > 0);
![]() }
            }
![]() }
        }
![]()
![]() public bool IsNextPage
        public bool IsNextPage
![]() {
        {
![]() get
            get
![]() {
            {
![]() return (PageIndex * PageSize) <=TotalCount;
                return (PageIndex * PageSize) <=TotalCount;
![]() }
            }
![]() }
        }
![]() }
    }
![]()
![]() public static class Pagination
    public static class Pagination
![]() {
    {
![]() public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
![]() {
        {
![]() return new PagedList<T>(source, index, pageSize);
            return new PagedList<T>(source, index, pageSize);
![]() }
        }
![]()
![]() public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
![]() {
        {
![]() return new PagedList<T>(source, index, 10);
            return new PagedList<T>(source, index, 10);
![]() }
        }
![]() }
    }
![]() }
}
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; using System.Collections.Generic;
using System.Collections.Generic; using System.Linq;
using System.Linq;
 namespace System.Web.Mvc
namespace System.Web.Mvc {
{ public interface IPagedList
    public interface IPagedList {
    { int TotalCount
        int TotalCount {
        { get;
            get; set;
            set; }
        }
 int PageIndex
        int PageIndex {
        { get;
            get; set;
            set; }
        }
 int PageSize
        int PageSize {
        { get;
            get; set;
            set; }
        }
 bool IsPreviousPage
        bool IsPreviousPage {
        { get;
            get; }
        }
 bool IsNextPage
        bool IsNextPage {
        { get;
            get; }
        } }
    }
 public class PagedList<T> : List<T>, IPagedList
    public class PagedList<T> : List<T>, IPagedList {
    { public PagedList(IQueryable<T> source, int index, int pageSize)
        public PagedList(IQueryable<T> source, int index, int pageSize) {
        { this.TotalCount = source.Count();
            this.TotalCount = source.Count(); this.PageSize = pageSize;
            this.PageSize = pageSize; this.PageIndex = index;
            this.PageIndex = index; this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList()); }
        }    
 public PagedList(List<T> source, int index, int pageSize)
        public PagedList(List<T> source, int index, int pageSize) {
        { this.TotalCount = source.Count();
            this.TotalCount = source.Count(); this.PageSize = pageSize;
            this.PageSize = pageSize; this.PageIndex = index;
            this.PageIndex = index; this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());
            this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList()); }
        }
 public int TotalCount
        public int TotalCount {
        { get; set;
            get; set; }
        }
 public int PageIndex
        public int PageIndex {
        { get; set;
            get; set; }
        }
 public int PageSize
        public int PageSize {
        { get; set;
            get; set; }
        }
 public bool IsPreviousPage
        public bool IsPreviousPage {
        { get
            get {
            { return (PageIndex > 0);
                return (PageIndex > 0); }
            } }
        }
 public bool IsNextPage
        public bool IsNextPage {
        { get
            get {
            { return (PageIndex * PageSize) <=TotalCount;
                return (PageIndex * PageSize) <=TotalCount; }
            } }
        } }
    }
 public static class Pagination
    public static class Pagination {
    { public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize)
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index, int pageSize) {
        { return new PagedList<T>(source, index, pageSize);
            return new PagedList<T>(source, index, pageSize); }
        }
 public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index)
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> source, int index) {
        { return new PagedList<T>(source, index, 10);
            return new PagedList<T>(source, index, 10); }
        } }
    } }
} 
                    
                



 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号