PagedList.cs using System.Collections.Generic; using System.Linq; using System; namespace System.Web.Mvc { public interface IPagedList { int TotalPage //总页数 { get; } 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) { if (index == null) { index = 1; } if (pageSize == null) { pageSize = 10; } this.TotalCount = source.Count(); this.PageSize = pageSize.Value; this.PageIndex = index.Value; this.AddRange(source.Skip((index.Value - 1) * pageSize.Value).Take(pageSize.Value)); } public int TotalPage { get { return (int)System.Math.Ceiling((double)TotalCount / PageSize); } } public int TotalCount { get; set; } public int PageIndex { get; set; } public int PageSize { get; set; } public bool IsPreviousPage { get { return (PageIndex > 1); } } public bool IsNextPage { get { return ((PageIndex) * PageSize) < TotalCount; } } } public static class Pagination { public static PagedList<T> ToPagedList<T>(this IOrderedQueryable<T> source, int? index, int? pageSize) { return new PagedList<T>(source, index, pageSize); } public static PagedList<T> ToPagedList<T>(this IOrderedQueryable<T> source, int? index) { return new PagedList<T>(source, index, 10); } 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); } } } MikePagerHtmlExtensions.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; using System.Text; namespace System.Web.Mvc { public static class MikePagerHtmlExtensions { #region MikePager 分页控件 public static string MikePager<T>(this HtmlHelper html, PagedList<T> data) { string actioinName = html.ViewContext.RouteData.GetRequiredString("action"); return MikePager<T>(html, data, actioinName); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, object values) { string actioinName = html.ViewContext.RouteData.GetRequiredString("action"); return MikePager<T>(html, data, actioinName, values); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action) { return MikePager<T>(html, data, action, null); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, object values) { string controllerName = html.ViewContext.RouteData.GetRequiredString("controller"); return MikePager<T>(html, data, action, controllerName, values); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, string controller, object values) { return MikePager<T>(html, data, action, controller, new RouteValueDictionary(values)); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, RouteValueDictionary values) { string actioinName = html.ViewContext.RouteData.GetRequiredString("action"); return MikePager<T>(html, data, actioinName, values); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, RouteValueDictionary values) { string controllerName = html.ViewContext.RouteData.GetRequiredString("controller"); return MikePager<T>(html, data, action, controllerName, values); } public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, string controller, RouteValueDictionary valuedic) { int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1; int end = (data.TotalPage - start) > 9 ? start + 9 : data.TotalPage; RouteValueDictionary vs = valuedic == null ? new RouteValueDictionary() : valuedic; var builder = new StringBuilder(); builder.AppendFormat("<div class=/"mike_mvc_pager/">"); if (data.IsPreviousPage) { vs["pageIndex"] = 1; builder.Append(Html.LinkExtensions.ActionLink(html, "首页", action, controller, vs, null)); builder.Append(" ");//add by 51aspx vs["pageIndex"] = data.PageIndex - 1; builder.Append(Html.LinkExtensions.ActionLink(html, "上一页", action, controller, vs, null)); builder.Append(" ");//add by 51aspx } for (int i = start; i <= end; i++) //前后各显示5个数字页码 { vs["pageIndex"] = i; if (i == data.PageIndex) { builder.Append(" <font class='thispagethis'>" + i.ToString() + "</font>"); } else { builder.Append(" ");//add by 51aspx builder.Append(Html.LinkExtensions.ActionLink(html, i.ToString(), action, controller, vs, null)); } } if (data.IsNextPage) { builder.Append(" ");//add by 51aspx vs["pageIndex"] = data.PageIndex + 1; builder.Append(Html.LinkExtensions.ActionLink(html, "下一页", action, controller, vs, null)); builder.Append(" ");//add by 51aspx vs["pageIndex"] = data.TotalPage; builder.Append(Html.LinkExtensions.ActionLink(html, "末页", action, controller, vs, null)); } builder.Append(" 每页" + data.PageSize + "条/共" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPage + "页 </div>"); return builder.ToString(); } #endregion } } Controller: public ActionResult Index(int? pageIndex) { int pagesize = 20; var dataContext = new MovieDataContext(); var movies = (from m in dataContext.Movies orderby m.id descending select m); PagedList<Movies> _movies = movies.ToPagedList(pageIndex, pagesize); return View(_movies); } View: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> <%@ Import Namespace="test.Models" %> <%@ Import Namespace="test.Code" %> <asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server"> Home Page </asp:Content> <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"> <%=Html.ActionLink("Add New", "edit", new { id=0, returnURL=Html.getURLString()})%> <table> <tr> <th align="left">Id</th><th align="left">Title</th><th align="left">Director</th><th align="left">Release Date</th><th align="center">操作</th><th align="center">操作</th> </tr> <% foreach (Movies m in (IEnumerable)ViewData.Model) { %> <tr> <td><%= m.id %></td> <td><%= Html.Encode(m.Title) %></td> <td><%= Html.Encode(m.Director) %></td> <td><%= m.DateReleased %></td> <td align="center"><%= Html.ActionLink("编辑", "edit", new { id=m.id , returnURL=Html.getURLString()})%></td> <td align="center"><%= Html.ActionLink("删除", "delete", new { id = m.id, returnURL = Html.getURLString() }, new { @onclick = "javascript:if(!confirm('您确定要删除?')){return false;}" })%></td> </tr> <% } %> </table> <%=Html.MikePager(ViewData.Model as PagedList<Movies>)%> </asp:Content> 源代码下载:http://download.youkuaiyun.com/source/1909864