实现效果如图:
1.新建一个类:PagerHelper,方法名称:Pager,实现代码:
public class PagerHelper { /// <summary> /// 生成页码的html /// </summary> /// <param name="urlFormat">超链接的格式。/Home?pagenum={pagenum}。地址中用{pagenum}做为当前页码的占位符</param> /// <param name="totalSize">总数据条数</param> /// <param name="pageSize">每页多少条数据 </param> /// <param name="currentPage">当前页的页码</param> /// <returns></returns> public static RawString Pager(string urlFormat, long totalSize,long pageSize, long currentPage) { StringBuilder sb = new StringBuilder(); //总页数 long totalPageCount = (long)Math.Ceiling((totalSize * 1.0f) / (pageSize * 1.0f)); //在当前页面前后各最多显示5个页码 int currentPageNum = 2; if (currentPage == 1 || currentPage == totalPageCount) { currentPageNum = 4; } else if (currentPage == 2 || currentPage == totalPageCount - 1) { currentPageNum = 3; } //计算页码条中第一条的页码 long firstPageNum = Math.Max(currentPage - currentPageNum, 1); //计算页码条中最后一条的页码 long lastPageNum = Math.Min(currentPage + currentPageNum, totalPageCount); sb.Append("<ul class='pagination col-md-12'>"); if (currentPage > 1) { sb.AppendLine("<li><a href='" + urlFormat.Replace("{pagenum}", "1") + "'>首页</a></li>"); sb.AppendLine("<li><a href='" + urlFormat.Replace("{pagenum}", (currentPage - 1).ToString()) + "'>上页</a></li>"); } else { sb.AppendLine("<li><a>首页</a></li>"); sb.AppendLine("<li><a>上页</a></li>"); } for (long i = firstPageNum; i <= lastPageNum; i++) { string url = urlFormat.Replace("{pagenum}", i.ToString()); if (i == currentPage) { sb.Append("<li class='active'><a>" + i + "</a></li>"); } else { sb.Append("<li><a href='" + url + "'>" + i + "</a></li>"); } } if (currentPage < totalPageCount) { sb.AppendLine("<li><a href='" + urlFormat.Replace("{pagenum}", (currentPage + 1).ToString()) + "'>下页</a></li>"); sb.AppendLine("<li><a href='" + urlFormat.Replace("{pagenum}", totalPageCount.ToString()) + "'>末页</a></li>"); } else { sb.AppendLine("<li><a>下页</a></li>"); sb.AppendLine("<li><a>末页</a></li>"); } sb.Append("</ul>"); return new RawString(sb.ToString()); } }
2.css样式:
<style type="text/css"> .pagination { display: inline-block; padding-left: 0; margin: 20px 0; border-radius: 4px; } .pagination > li { display: inline; } .pagination > li > a, .pagination > li > span { position: relative; float: left; padding: 3px 10px; margin-left: -1px; line-height: 1.42857143; color: #337ab7; text-decoration: none; background-color: #fff; border: 1px solid #ddd; } .pagination > li:first-child > a, .pagination > li:first-child > span { margin-left: 0; border-top-left-radius: 4px; border-bottom-left-radius: 4px; } .pagination > li:last-child > a, .pagination > li:last-child > span { border-top-right-radius: 4px; border-bottom-right-radius: 4px; } .pagination > li > a:hover, .pagination > li > span:hover, .pagination > li > a:focus, .pagination > li > span:focus { z-index: 3; color: #23527c; background-color: #eee; border-color: #ddd; } .pagination > .active > a, .pagination > .active > span, .pagination > .active > a:hover, .pagination > .active > span:hover, .pagination > .active > a:focus, .pagination > .active > span:focus { z-index: 2; color: #fff; cursor: default; background-color: #337ab7; border-color: #337ab7; } .pagination > .disabled > span, .pagination > .disabled > span:hover, .pagination > .disabled > span:focus, .pagination > .disabled > a, .pagination > .disabled > a:hover, .pagination > .disabled > a:focus { color: #777; cursor: not-allowed; background-color: #fff; border-color: #ddd; } .pagination-lg > li > a, .pagination-lg > li > span { padding: 10px 16px; font-size: 18px; line-height: 1.3333333; } .pagination-lg > li:first-child > a, .pagination-lg > li:first-child > span { border-top-left-radius: 6px; border-bottom-left-radius: 6px; } .pagination-lg > li:last-child > a, .pagination-lg > li:last-child > span { border-top-right-radius: 6px; border-bottom-right-radius: 6px; } .pagination-sm > li > a, .pagination-sm > li > span { padding: 5px 10px; font-size: 12px; line-height: 1.5; } .pagination-sm > li:first-child > a, .pagination-sm > li:first-child > span { border-top-left-radius: 3px; border-bottom-left-radius: 3px; } .pagination-sm > li:last-child > a, .pagination-sm > li:last-child > span { border-top-right-radius: 3px; border-bottom-right-radius: 3px; } </style>
3.调用:
@Html.Raw(Tools.PagerHelper.Pager("/Home?pn={pagenum}", Model.TotalSize, 10, Model.PageNum))