一个分页的HtmlHelper
1 namespace System.Web.Mvc 2 { 3 public static class TianqyHtmlHelper 4 { 5 /// <summary> 6 /// 分页HtmlHelper 7 /// </summary> 8 public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount) 9 { 10 pageSize = pageSize == 0 ? 3 : pageSize; 11 var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; 12 var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数 13 var outPut = new StringBuilder(); 14 if (totalPages > 1) 15 { 16 //首页 17 if (currentPage != 1) 18 { 19 outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex=1&pageSize={1}‘>首页</a>", redirectTo, pageSize); 20 } 21 outPut.Append(" "); 22 //上一页 23 if (currentPage > 1) 24 { 25 outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>上一页</a>", redirectTo, currentPage - 1, pageSize); 26 } 27 outPut.Append(" "); 28 //一共最多显示10个页码,前5个,后5个 29 int n = 5; 30 for (int i = 0; i <= 10; i++) 31 { 32 if ((currentPage + i - n) > 1 && (currentPage + i - n) <= totalPages) 33 { 34 if (i == n) 35 { 36 outPut.AppendFormat("<a class=‘pageCurrent‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a>", redirectTo, currentPage, pageSize, currentPage); 37 } 38 else 39 { 40 outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a>", redirectTo, currentPage + i - n, pageSize, currentPage + i - n); 41 } 42 } 43 outPut.Append(" "); 44 } 45 //下一页 46 if (currentPage < totalPages) 47 { 48 outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>下一页</a>", redirectTo, currentPage + 1, pageSize); 49 } 50 outPut.Append(" "); 51 //末页 52 if (currentPage != totalPages) 53 { 54 outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>末页</a>", redirectTo, totalPages, pageSize); 55 } 56 outPut.Append(" "); 57 } 58 outPut.AppendFormat("第{0}页/共{1}页", currentPage, totalPages); 59 return new HtmlString(outPut.ToString()); 60 } 61 } 62 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。