MVC中的自定义控件——分页
上一篇是个简单例子,这篇借分页写个升级版的例子。
不想拼html代码,因为不好修改,那写一个PartialView.
@model System.Web.Mvc.PagerModel @if (Model.PagerShow) { <div id="@Model.PagerId" class="pagination pagination-right"> <ul> <li class="@((Model.CurTeam == 1)?"disabled":"")"><a href="?p=1"><i class="icon-fast-backward"></i></a></li> <li class="@((Model.CurTeam == 1)?"disabled":"")"><a href="?p=@Model.PreStep"><i class="icon-backward"></i></a></li> @foreach (var item in Model.PageList) { <li class="@((item.PageNumber == Model.CurPage)?"active":"")"><a href="?p=@item.PageNumber" >@item.PageNumber</a></li> } <li class="@((Model.CurTeam == Model.TotalTeam)?"disabled":"")"><a href="?p=@Model.NextStep"><i class="icon-forward"></i></a></li> <li class="@((Model.CurTeam == Model.TotalTeam)?"disabled":"")"><a href="?p=@Model.TotalPage"><i class="icon-fast-forward"></i></a></li> </ul> </div> }
然后写扩展方法,顺带PartialView的Model。用HtmlHelper的Partial生成HtmlString。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.Routing; using System.Text; namespace System.Web.Mvc { /// <summary> /// 分页控件属性 /// </summary> public class PagerModel { /// <summary> /// 页码控件ID /// </summary> public string PagerId { get; set; } /// <summary> /// 页码空控件开关 /// </summary> public bool PagerShow { get; set; } /// <summary> /// 当前分组,从1开始 /// </summary> public int CurTeam { get; set; } /// <summary> /// 前一跳页码 /// </summary> public int PreStep { get; set; } /// <summary> /// 当前页码,从1开始 /// </summary> public int CurPage { get; set; } /// <summary> /// 后一跳页码 /// </summary> public int NextStep { get; set; } /// <summary> /// 总页数 /// </summary> public int TotalPage { get; set; } /// <summary> /// 页码分组数 /// </summary> public int TotalTeam { get; set; } /// <summary> /// 当前页码组 /// </summary> public List<PageModel> PageList { get; set; } } /// <summary> /// 页码属性 /// </summary> public class PageModel { /// <summary> /// 页码 /// </summary> public int PageNumber { get; set; } } public static class PageHelper { public static MvcHtmlString Pager(this HtmlHelper helper, string pagerId, //分页控件Id int curPage, //当前页 int pagerSize, //页码个数 int totalSize, //总记录数 int pageSize, //每页记录数 string view //分页模板 ) { if (curPage<1) { curPage = 1; } if (totalSize<1) { totalSize = 1; } PagerModel pager = new PagerModel(); pager.PagerId = pagerId; pager.CurPage = curPage; pager.TotalPage = (totalSize % pageSize == 0) ? (totalSize / pageSize) : (totalSize / pageSize) + 1; if (curPage > pager.TotalPage) { curPage = pager.TotalPage; } int curTeam = (curPage % pagerSize == 0) ? (curPage / pagerSize) : (curPage / pagerSize) + 1; //页码当前分组 pager.TotalTeam = (pager.TotalPage % pagerSize == 0) ? (pager.TotalPage / pagerSize) : (pager.TotalPage / pagerSize) + 1; //页码当前分组 pager.CurTeam = curTeam; int leftPageNum = (curTeam - 1)* pagerSize + 1;//左边界 pager.PreStep = (leftPageNum - pagerSize) > 1 ? (leftPageNum - pagerSize) : 1; pager.NextStep = (leftPageNum + pagerSize) <= pager.TotalPage ? (leftPageNum + pagerSize) : leftPageNum; if (pager.TotalPage > 1) { pager.PagerShow = true;//显示分页 List<PageModel> pageList = new List<PageModel>(); int step = (leftPageNum + pagerSize) < pager.TotalPage ? pagerSize : pager.TotalPage % pageSize ; var sourceList = Enumerable.Range(leftPageNum, step); pageList.AddRange(sourceList.Select(p => new PageModel{PageNumber = p})); pager.PageList = pageList; } else { pager.PagerShow = false;//页数少于一页,则不显示分页 } return helper.Partial(view, pager); } } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。