Model:
public class PaggerModel { public PaggerModel() { BarSize = 10; } public PaggerModel(int total, int page, int size) : this() { TotalCount = total; CurrentPageIndex = page; PageSize = size; } public PaggerModel(int total, int page, int size, string action, string controller) : this(total, page, size) { ActionName = action; ControllerName = controller; } public string ActionName { get; set; } public int BarSize { get; set; } public string ControllerName { get; set; } /// <summary> /// 当前页码 /// </summary> public int CurrentPageIndex { get { return currentPageIndex; } set { currentPageIndex = (value < 1 ? 1 : value); } } /// <summary> /// 最后一页页码 /// </summary> public int LastPageIndex { get { if (PageSize == 0) { return (int)Math.Ceiling((double)TotalCount / 10); } return (int)Math.Ceiling((double)TotalCount / PageSize) == 0 ? 1 : (int)Math.Ceiling((double)TotalCount / PageSize); } } /// <summary> /// 单个页面显示条目 /// </summary> public int PageSize { get { return pageSize; } set { pageSize = (value < 1 ? 1 : value); } } /// <summary> /// 总条目 /// </summary> public int TotalCount { get; set; } private int currentPageIndex { get; set; } private int pageSize { get; set; } }
View:
以分部视图的方式渲染:
@Html.Partial("PaggerModel", ViewBag.PaggerModel as PaggerModel)
PaggerModel.cshtml:
@model PaggerModel @{ var config = ViewData["Config"] as PaggerModelConfig; var configcss = ""; if (config != null) { configcss = "pager-init"; } else { config = new PaggerModelConfig { }; } int current = Model.CurrentPageIndex; var pageSize = Model.PageSize == 0 ? 10 : Model.PageSize; var half = (Model.BarSize - 1) / 2; var fi = current - half; var li = current + Model.BarSize - 1 - half; li = li > Model.LastPageIndex ? Model.LastPageIndex : li; if (fi < 1) { fi = 1; li = Model.LastPageIndex < Model.BarSize ? Model.LastPageIndex : Model.BarSize; } while (fi > 1 && li - fi + 1 < Model.BarSize) { --fi; } var maxrow = Model.TotalCount > 200 ? 200 : Model.TotalCount; if (maxrow == 0) { maxrow = 1; } } <div class="turn_page_wrap"> <ul class="turn_page_bar df"> <li class="first_page"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p=1, size=pageSize })">首页</a></li> @if (current > 1) { <li class="page_num"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p=current-1, size=pageSize })">«</a></li> } else { <li class="page_num on"><a href="javascript:void(0)">«</a></li> } @for (int i = fi; i <= li; i++) { if (i == current) { <li class="page_num on"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p = i, size = pageSize })">@i</a></li> } else { <li class="page_num"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p = i, size = pageSize })">@i</a></li> } } @if (current < Model.LastPageIndex) { <li class="page_num"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p = current + 1, size = pageSize })">»</a></li> } else { <li class="page_num on"><a href="javascript:void(0)">»</a></li> } <li class="last_page"><a href="@Url.QAction(Model.ActionName, Model.ControllerName, new { p=Model.LastPageIndex, size=pageSize })">尾页</a></li> </ul> </div>