博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用HtmlHelper 写自己的 mvc 分页
阅读量:6324 次
发布时间:2019-06-22

本文共 8223 字,大约阅读时间需要 27 分钟。

最近项目中用到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); }        }    }
View Code

 

分页扩展

///         /// 根据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(); }
View Code

 

定义一个model

public class PhotoAlbumReply : IReply    {        public string Id { get; set; }        public string Content { get; set; }    }
View Code

  

定义一个viewmodel

public class PhotoAlbumReplyViewModel    {        public IEnumerable
PhotoAlbumReplys { get; set; } public PagingInfo PagingInfo { get; set; } }
View Code

 

写自己的controller

public ActionResult List(int page)        {            int pageSize = 20;            int count = 100;//这个自己去实现            IEnumerable
PhotoAlbumReplys = null;//这个自己去实现 return View( new PhotoAlbumReplyViewModel() { PhotoAlbumReplys = PhotoAlbumReplys, PagingInfo = new PagingInfo() { IsShowFirstLast = true, ShowNum = 4, CurrentPage = page, ItemsPerPage = pageSize, TotalItems = count } }); }
View Code

 

view 调用

@{    ViewBag.Title = "List";}@model ViewModel.PhotoAlbumReplyViewModel @if (null != Model.PhotoAlbumReplys && Model.PhotoAlbumReplys.Count() > 0)    {
foreach (var item in Model.PhotoAlbumReplys) {
}
ID content
@item.Id @item.TargetId @item.Content
}
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", "Photo", new {page=x}))
View Code

打完收工。 

转载于:https://www.cnblogs.com/elent/p/3514857.html

你可能感兴趣的文章
在 Web 页面中使用离线地图
查看>>
搭建 Docker-Registry 私有仓库
查看>>
jquery选择器
查看>>
如何提高编程能力
查看>>
Oracle执行计划
查看>>
js 时间格式化 兼容safari 苹果手机
查看>>
Yii 中,render 和 renderPartial 的区别[转]
查看>>
第67天:面向对象的声明、封装
查看>>
51nod 1105 第K大的数
查看>>
javaScript异常示范案例
查看>>
Android中如何实现EditText的自动换行
查看>>
01-Scrum 概述
查看>>
bzoj 4556 [Tjoi2016&Heoi2016]字符串——后缀数组+主席树
查看>>
TensorFlow+实战Google深度学习框架学习笔记(10)-----神经网络几种优化方法
查看>>
SECTION 4: THE TIMEQUEST GUI
查看>>
window's chkdsk
查看>>
WCF序列化
查看>>
Host+DLL模式下快捷键的使用
查看>>
notepad++添加Compare插件
查看>>
[转] Mac OS X 10.6下安装MySQL 5.1.45
查看>>