首页 > 代码库 > 一个分页的HtmlHelper
一个分页的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=http://www.mamicode.com/‘{0}?pageIndex=1&pageSize={1}‘>首页", redirectTo, pageSize);20 }21 outPut.Append(" ");22 //上一页23 if (currentPage > 1)24 {25 outPut.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>上一页", 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=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>{3}", redirectTo, currentPage, pageSize, currentPage);37 }38 else39 {40 outPut.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>{3}", 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=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>下一页", redirectTo, currentPage + 1, pageSize);49 }50 outPut.Append(" ");51 //末页52 if (currentPage != totalPages)53 {54 outPut.AppendFormat("<a class=‘pageLink‘ href=http://www.mamicode.com/‘{0}?pageIndex={1}&pageSize={2}‘>末页", redirectTo, totalPages, pageSize);55 }56 outPut.Append(" ");57 }58 outPut.AppendFormat("第{0}页/共{1}页", currentPage, totalPages);59 return new HtmlString(outPut.ToString());60 }61 }62 }
一个分页的HtmlHelper
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。