最近项目中用到MVC3,很多分页,本人习惯自己自己去实现一些代码,看了很多控件,总感觉代码有点多,还要去应用他们的DLL。甚是繁琐。所以就自己实现一下。
定义分页信息类
////// 分页信息 /// public class PagingInfo { ////// 显示连续页码数量 /// public int ShowNum { get; set; } ////// 记录总数 /// public int TotalItems { get; set; } ////// 每页记录数 /// public int ItemsPerPage { get; set; } ////// 当前页 /// public int CurrentPage { get; set; } ////// 是否显示第一页和最后一页 /// private bool _IsShowFirstLast = true; public bool IsShowFirstLast { get { return _IsShowFirstLast; } set { _IsShowFirstLast = value; } } public int TotalPages { get { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); } } }
分页扩展
////// 根据pageInfo 生成一个分页控件 /// /// /// /// ///public static MvcHtmlString PageLinks(this HtmlHelper html, PagingInfo pageInfo, Func pageUrl) { StringBuilder result = new StringBuilder(1000); bool isEven = (pageInfo.ShowNum & 1).Equals(0); int middleCeilingNum = (int)Math.Ceiling((decimal)pageInfo.ShowNum / 2); int middleFloorNum = (int)Math.Floor((decimal)pageInfo.ShowNum / 2); middleFloorNum = middleFloorNum == 0 ? 1 : middleFloorNum; int beginIndex = 1;//控件开始页数 int endIndex = 1;//控件结束页数 if (pageInfo.ShowNum >= pageInfo.TotalPages) { beginIndex = 1; endIndex = pageInfo.TotalPages; } else { if (!isEven) { beginIndex = pageInfo.CurrentPage <= middleCeilingNum ? 1 : pageInfo.CurrentPage - middleFloorNum; endIndex = pageInfo.CurrentPage <= middleCeilingNum ? pageInfo.ShowNum : pageInfo.CurrentPage + middleFloorNum; beginIndex = pageInfo.CurrentPage + middleCeilingNum > pageInfo.TotalPages ? pageInfo.TotalPages - pageInfo.ShowNum + 1 : beginIndex; endIndex = pageInfo.CurrentPage + middleCeilingNum > pageInfo.TotalPages ? pageInfo.TotalPages : endIndex; } else { beginIndex = pageInfo.CurrentPage <= middleCeilingNum ? 1 : pageInfo.CurrentPage - middleFloorNum + 1; endIndex = pageInfo.CurrentPage <= middleCeilingNum ? pageInfo.ShowNum : pageInfo.CurrentPage + middleFloorNum; beginIndex = pageInfo.CurrentPage + middleCeilingNum > pageInfo.TotalPages ? pageInfo.TotalPages - pageInfo.ShowNum + 1 : beginIndex; endIndex = pageInfo.CurrentPage + middleCeilingNum > pageInfo.TotalPages ? pageInfo.TotalPages : endIndex; } } beginIndex = pageInfo.CurrentPage == pageInfo.ShowNum - middleFloorNum + 1 ? beginIndex - 1 : beginIndex; beginIndex = pageInfo.CurrentPage == pageInfo.TotalPages + pageInfo.ShowNum - middleFloorNum ? beginIndex + 1 : beginIndex; endIndex = pageInfo.CurrentPage == pageInfo.TotalPages - middleFloorNum - 1 ? endIndex + 1 : endIndex; if (pageInfo.TotalPages <= pageInfo.ShowNum + 2) { result.Append(GetMiddle(pageInfo, beginIndex, endIndex, pageUrl)); } else { //前一页 if (pageInfo.CurrentPage != 1) result.Append(GetUrl(pageInfo, pageUrl, pageInfo.CurrentPage - 1, "上一页").ToString()); //第一页 if (pageInfo.CurrentPage > middleCeilingNum + 1 && pageInfo.IsShowFirstLast) result.Append(GetUrl(pageInfo, pageUrl, 1, "1").ToString()); if (pageInfo.CurrentPage > middleCeilingNum && pageInfo.CurrentPage != pageInfo.ShowNum - middleFloorNum + 1) result.Append(GetSplitStr().ToString()); result.Append(GetMiddle(pageInfo, beginIndex, endIndex, pageUrl)); if (pageInfo.CurrentPage < pageInfo.TotalPages - middleFloorNum && pageInfo.CurrentPage != pageInfo.TotalPages - middleFloorNum - 1) result.Append(GetSplitStr().ToString()); //最后一页 if ( pageInfo.CurrentPage < pageInfo.TotalPages - (middleFloorNum + 1) && pageInfo.IsShowFirstLast) result.Append(GetUrl(pageInfo, pageUrl, pageInfo.TotalPages, pageInfo.TotalPages + "").ToString()); //下一页 if (pageInfo.TotalPages != 1 && (pageInfo.CurrentPage != pageInfo.TotalPages)) result.Append(GetUrl(pageInfo, pageUrl, pageInfo.CurrentPage + 1, "下一页").ToString()); } return MvcHtmlString.Create(result.ToString()); } /// /// 特殊页面(第一页,前一页,后一页,最后一页)的连接生成 /// /// /// /// /// 显示文字 ///private static TagBuilder GetUrl(PagingInfo pageInfo, Func pageUrl, int i, string ShowTag) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = ShowTag; return tag; } private static TagBuilder GetSplitStr() { TagBuilder tag = new TagBuilder("span"); tag.MergeAttribute("class", "psplit"); tag.InnerHtml = "..."; return tag; } /// /// /// ///private static string GetMiddle(PagingInfo pageInfo, int beginIndex, int endIndex, Func pageUrl) { StringBuilder result = new StringBuilder(300); for (int i = beginIndex; i <= endIndex; i++) { TagBuilder tag = new TagBuilder("a"); tag.MergeAttribute("href", pageUrl(i)); tag.InnerHtml = i.ToString(); if (i == pageInfo.CurrentPage) tag.AddCssClass("selected"); result.Append(tag.ToString()); } return result.ToStr(); }
定义一个model
public class PhotoAlbumReply : IReply { public string Id { get; set; } public string Content { get; set; } }
定义一个viewmodel
public class PhotoAlbumReplyViewModel { public IEnumerablePhotoAlbumReplys { get; set; } public PagingInfo PagingInfo { get; set; } }
写自己的controller
public ActionResult List(int page) { int pageSize = 20; int count = 100;//这个自己去实现 IEnumerablePhotoAlbumReplys = null;//这个自己去实现 return View( new PhotoAlbumReplyViewModel() { PhotoAlbumReplys = PhotoAlbumReplys, PagingInfo = new PagingInfo() { IsShowFirstLast = true, ShowNum = 4, CurrentPage = page, ItemsPerPage = pageSize, TotalItems = count } }); }
view 调用
@{ ViewBag.Title = "List";}@model ViewModel.PhotoAlbumReplyViewModel @if (null != Model.PhotoAlbumReplys && Model.PhotoAlbumReplys.Count() > 0) {
ID | content | |
@item.Id | @item.TargetId | @item.Content |
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", "Photo", new {page=x}))
打完收工。