using System; using System.Collections.Generic; using System.Linq; using System.Web; using Webdiyer.WebControls.Mvc; namespace MvcGuestBook.Common { public static class MvcPaper { /// <summary> /// 重写PagedList可以使用List集合数据 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="allItems">IList集合</param> /// <param name="pageIndex">页码</param> /// <param name="pageSize">一页显示的数量</param> /// <returns>PagedList<T></returns> public static PagedList<T> ToPagedList<T>(this IList<T> allItems, int pageIndex, int pageSize) { if (pageIndex < 1) pageIndex = 1; var itemIndex = (pageIndex - 1) * pageSize; var pageOfItems = allItems.Skip(itemIndex).Take(pageSize); var totalItemCount = allItems.Count(); return new PagedList<T>(pageOfItems, pageIndex, pageSize, totalItemCount); } } } public ActionResult IndexPage(int? id = 1) { IList<Message> userList = new List<Message>(); int totalCount = 0; int pageIndex = id ?? 1; userList = Common.Common.TableToList<Message>(_messageRepository.GetLists("").Tables[0]); //var queryable = userList.AsQueryable(); //userList = SC.Repository.User.GetList("", 2, (pageIndex - 1) * 2, out totalCount); PagedList<Message> mPage = Common.MvcPaper.ToPagedList<Message>(userList, pageIndex, 15); totalCount = userList.Count; mPage.TotalItemCount = totalCount; mPage.CurrentPageIndex = (int)(id ?? 1); return View(mPage); } <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PagedList<MvcGuestBook.Models.Message>>" %> <%@ Import Namespace="Webdiyer.WebControls.Mvc"%> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> IndexPage </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>IndexPage</h2> <table> <tr> <th></th> <th> ID </th> <th> MemberID </th> <th> Body </th> <th> IsSecret </th> <th> AdminReply </th> <th> AdminReplyTime </th> <th> CreateTime </th> </tr> <% foreach (var item in Model) { %> <tr> <td> <%: Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> | <%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ })%> | <%: Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })%> </td> <td> <%: item.ID %> </td> <td> <%: item.MemberID %> </td> <td> <%: item.Body %> </td> <td> <%: item.IsSecret %> </td> <td> <%: item.AdminReply %> </td> <td> <%: String.Format("{0:g}", item.AdminReplyTime) %> </td> <td> <%: String.Format("{0:g}", item.CreateTime) %> </td> </tr> <% } %> </table> <%: Html.Pager(Model, new PagerOptions { PageIndexParameterName="ID",ShowPageIndexBox=true, FirstPageText="首页",PrevPageText="上一页",NextPageText="下一页",LastPageText="末页",PageIndexBoxType=PageIndexBoxType.TextBox, PageIndexBoxWrapperFormatString="请输入页数{0}",GoButtonText="转到"})%> <br /> >>分页 共有 <%: Model.TotalItemCount %> 篇留言 <%: Model.CurrentPageIndex %>/<%: Model.TotalPageCount %> <p> <%: Html.ActionLink("Create New", "Create") %> </p> <p> <%: Html.ActionLink("返回第一种分页方法", "Index") %> </p> </asp:Content> 详细源码示例链接: http://download.csdn.net/download/u012949335/10189580