您可以使用圍繞列表by Rob Conery的PagedList
包裝。還有一個由Troy Goode擴展版本。
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);
}
}
}
看起來很完美,這正是我一直在尋找 – 2010-03-03 22:44:46
在第二腮紅,它缺少一些項目,但它是一個良好的開端。 – 2010-03-03 22:50:20
增加了一個稍微擴展版本的鏈接。不過,我認爲你可以從這個基地實施你的要求。 – AxelEckenberger 2010-03-03 22:58:01