文章出處
PagerModel
PagerHelper
PagedListModel
Controller
文章列表
asp.net mvc 自定義Pager實現分頁
Intro
一個WEB應用程序中經常會用到數據分頁,本文將實現一個簡單通用的分頁組件,包含一個 PagerModel
(用來保存頁碼信息),一個 HtmlHelper
的 Pager
擴展方法和一個 PagedListModel<T>
分頁數據模型。
pager效果圖如下:
PagerModel 分頁模型
PagerModel 用來保存分頁信息,代碼實現如下:

1 /// <summary> 2 /// PagerModel 分頁模型 3 /// </summary> 4 public class PagerModel 5 { 6 public int PageIndex { get; set; } 7 8 public int PageSize { get; set; } 9 10 public int PageCount { get; private set; } 11 12 public int TotalCount { get; set; } 13 14 public PagerModel(int totalCount) 15 { 16 PageIndex = 1; 17 PageSize = 10; 18 TotalCount = totalCount; 19 PageCount = Convert.ToInt32(Math.Ceiling(TotalCount * 1.0 / PageSize)); 20 } 21 22 public PagerModel(int pageSize, int totalCount) 23 { 24 PageIndex = 1; 25 PageSize = pageSize; 26 TotalCount = totalCount; 27 PageCount = Convert.ToInt32(Math.Ceiling(TotalCount * 1.0 / PageSize)); 28 } 29 30 public PagerModel(int pageIndex, int pageSize, int totalCount) 31 { 32 PageIndex = pageIndex; 33 PageSize = pageSize; 34 TotalCount = totalCount; 35 PageCount = Convert.ToInt32(Math.Ceiling(TotalCount * 1.0 / PageSize)); 36 } 37 }
/// <summary> /// PagerModel 分頁模型 /// </summary> public class PagerModel { public int PageIndex { get; set; } public int PageSize { get; set; } public int PageCount { get; private set; } public int TotalCount { get; set; } public PagerModel(int totalCount) { PageIndex = 1; PageSize = 10; TotalCount = totalCount; PageCount = Convert.ToInt32(Math.Ceiling(TotalCount * 1.0 / PageSize)); } public PagerModel(int pageSize, int totalCount) { PageIndex = 1; PageSize = pageSize; TotalCount = totalCount; PageCount = Convert.ToInt32(Math.Ceiling(TotalCount * 1.0 / PageSize)); } public PagerModel(int pageIndex, int pageSize, int totalCount) { PageIndex = pageIndex; PageSize = pageSize; TotalCount = totalCount; PageCount = Convert.ToInt32(Math.Ceiling(TotalCount * 1.0 / PageSize)); } }
Pager擴展方法
Pager擴展方法 可根據自己的需求進行定制話實現,Pager擴展方法需要一個PagerModel類型的參數來生成頁碼,我這里是用的bootstrap的樣式來做的分頁,整個系統用的是同一個樣式的分頁所以把樣式直接寫死在代碼里了。
Pager這里的代碼重構做的不是太好:
- 樣式直接寫死,前端不好維護

1 /// <summary> 2 /// PagerHelper 分頁幫助類 3 /// </summary> 4 public static class PagerHelper 5 { 6 public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager) 7 { 8 StringBuilder sbHtmlText = new StringBuilder(); 9 sbHtmlText.Append("<div style=\"text-align:center\"><nav><ul class=\"pagination\">"); 10 if (pager.PageIndex <= 1) 11 { 12 sbHtmlText.Append("<li class=\"disabled\"><a href=\"javascript:void()\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"); 13 } 14 else 15 { 16 sbHtmlText.AppendFormat("<li><a href=\"javascript:loadData(1)\" aria-label=\"1\"><span aria-hidden=\"true\">«</span></a></li>", pager.PageIndex - 1); 17 sbHtmlText.AppendFormat("<li><a href=\"javascript:loadData({0})\">{0}</a></li>", pager.PageIndex - 1); 18 } 19 sbHtmlText.AppendFormat("<li class=\"active\"><a href=\"javascript:void()\">{0}<span class=\"sr-only\">(current)</span></a></li>", pager.PageIndex); 20 if (pager.PageIndex >= pager.PageCount) 21 { 22 sbHtmlText.Append("<li class=\"disabled\"><a href=\"javascript:void()\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"); 23 } 24 else 25 { 26 sbHtmlText.AppendFormat("<li><a href=\"javascript:loadData({0})\">{0}</a></li>", pager.PageIndex + 1); 27 sbHtmlText.AppendFormat("<li><a href=\"javascript:loadData({0})\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>", pager.PageIndex + 1); 28 } 29 sbHtmlText.Append("</ul></nav>"); 30 sbHtmlText.AppendFormat("<div><span>每頁有<strong>{0}</strong>條數據,一共有<strong>{1}</strong>頁,總計<strong>{2}</strong>條數據</span></div></div>", pager.PageSize, pager.PageCount, pager.TotalCount); 31 return MvcHtmlString.Create(sbHtmlText.ToString()); 32 } 33 34 public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager, Func<int, string> onPageChange) 35 { 36 StringBuilder sbHtmlText = new StringBuilder(); 37 sbHtmlText.Append("<div style=\"text-align:center\"><nav><ul class=\"pagination\">"); 38 if (pager.PageIndex <= 1) 39 { 40 sbHtmlText.Append("<li class=\"disabled\"><a href=\"javascript:void()\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"); 41 } 42 else 43 { 44 sbHtmlText.AppendFormat("<li><a href=\"{0}\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>", onPageChange(pager.PageIndex - 1)); 45 sbHtmlText.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", onPageChange(pager.PageIndex - 1), pager.PageIndex - 1); 46 } 47 sbHtmlText.AppendFormat("<li class=\"active\"><a href=\"javascript:void()\">{0}<span class=\"sr-only\">(current)</span></a></li>", pager.PageIndex); 48 if (pager.PageIndex >= pager.PageCount) 49 { 50 sbHtmlText.Append("<li class=\"disabled\"><a href=\"javascript:void()\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"); 51 } 52 else 53 { 54 sbHtmlText.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", onPageChange(pager.PageIndex+1),pager.PageIndex + 1); 55 sbHtmlText.AppendFormat("<li><a href=\"{0}\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>", onPageChange(pager.PageIndex + 1)); 56 } 57 sbHtmlText.Append("</ul></nav>"); 58 sbHtmlText.AppendFormat("<div><span>每頁有<strong>{0}</strong>條數據,一共有<strong>{1}</strong>頁,總計<strong>{2}</strong>條數據</span></div></div>", pager.PageSize, pager.PageCount, pager.TotalCount); 59 return MvcHtmlString.Create(sbHtmlText.ToString()); 60 } 61 }
/// <summary> /// PagerHelper 分頁幫助類 /// </summary> public static class PagerHelper { public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager) { StringBuilder sbHtmlText = new StringBuilder(); sbHtmlText.Append("<div style=\"text-align:center\"><nav><ul class=\"pagination\">"); if (pager.PageIndex <= 1) { sbHtmlText.Append("<li class=\"disabled\"><a href=\"javascript:void()\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=\"javascript:loadData({0})\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>", pager.PageIndex - 1); sbHtmlText.AppendFormat("<li><a href=\"javascript:loadData({0})\">{0}</a></li>", pager.PageIndex - 1); } sbHtmlText.AppendFormat("<li class=\"active\"><a href=\"javascript:void()\">{0}<span class=\"sr-only\">(current)</span></a></li>", pager.PageIndex); if (pager.PageIndex >= pager.PageCount) { sbHtmlText.Append("<li class=\"disabled\"><a href=\"javascript:void()\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=\"javascript:loadData({0})\">{0}</a></li>", pager.PageIndex + 1); sbHtmlText.AppendFormat("<li><a href=\"javascript:loadData({0})\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>", pager.PageIndex + 1); } sbHtmlText.Append("</ul></nav>"); sbHtmlText.AppendFormat("<div><span>每頁有<strong>{0}</strong>條數據,一共有<strong>{1}</strong>頁,總計<strong>{2}</strong>條數據</span></div></div>", pager.PageSize, pager.PageCount, pager.TotalCount); return MvcHtmlString.Create(sbHtmlText.ToString()); } }
利用 Func<> 簡單重構了一下代碼,可以自定義翻頁事件
public static MvcHtmlString Pager(this HtmlHelper helper, PagerModel pager, Func<int, string> onPageChange) { StringBuilder sbHtmlText = new StringBuilder(); sbHtmlText.Append("<div style=\"text-align:center\"><nav><ul class=\"pagination\">"); if (pager.PageIndex <= 1) { sbHtmlText.Append("<li class=\"disabled\"><a href=\"javascript:void()\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=\"{0}\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>", onPageChange(pager.PageIndex - 1)); sbHtmlText.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", onPageChange(pager.PageIndex - 1), pager.PageIndex - 1); } sbHtmlText.AppendFormat("<li class=\"active\"><a href=\"javascript:void()\">{0}<span class=\"sr-only\">(current)</span></a></li>", pager.PageIndex); if (pager.PageIndex >= pager.PageCount) { sbHtmlText.Append("<li class=\"disabled\"><a href=\"javascript:void()\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>"); } else { sbHtmlText.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", onPageChange(pager.PageIndex+1),pager.PageIndex + 1); sbHtmlText.AppendFormat("<li><a href=\"{0}\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span></a></li>", onPageChange(pager.PageIndex + 1)); } sbHtmlText.Append("</ul></nav>"); sbHtmlText.AppendFormat("<div><span>每頁有<strong>{0}</strong>條數據,一共有<strong>{1}</strong>頁,總計<strong>{2}</strong>條數據</span></div></div>", pager.PageSize, pager.PageCount, pager.TotalCount); return MvcHtmlString.Create(sbHtmlText.ToString()); }
關于樣式修改維護的問題,找到一種解決方案,可以將分頁代碼的樣式封裝在一個分部視圖partial,在需要分頁的頁面視圖,引用這個分部視圖并且將分頁模型信息 PagerModel 傳遞給這個分部視圖
PagedListModel 分頁數據模型
PagedListModel 用來封裝返回到視圖 View 的數據和 分頁模型 PagerModel,實現代碼如下

1 /// <summary> 2 /// 數據分頁模型 3 /// </summary> 4 /// <typeparam name="T">數據類型</typeparam> 5 public class PagedListModel<T> where T : class, new() 6 { 7 public List<T> Data { get; set; } 8 9 public PagerModel Pager { get; set; } 10 }
public class PagedListModel<T> where T : class, new() { public List<T> Data { get; set; } public PagerModel Pager { get; set; } }
控制器數據處理
控制器處理代碼如下:
search 是一個包含了 PageIndex 和 PageSize 的請求參數

1 int rowsCount = 0; 2 try 3 { 4 List<Models.BlockEntity> blockList = BusinessHelper.BlockEntityHelper.GetPagedList(search.PageIndex, search.PageSize, out rowsCount, whereLambda, b => b.BlockTime, false); 5 PagerModel pager = new PagerModel(search.PageIndex, search.PageSize, rowsCount); 6 PagedListModel<Models.BlockEntity> dataList = new PagedListModel<Models.BlockEntity>() { Pager = pager, Data = blockList }; 7 return View(dataList); 8 } 9 catch (Exception ex) 10 { 11 logger.Error(ex); 12 throw; 13 }
int rowsCount = 0; try { List<Models.BlockEntity> blockList = new Business.BLLBlockEntity().GetPagedList(search.PageIndex, search.PageSize, out rowsCount, whereLambda, b => b.BlockTime, false);//記錄日志 PagerModel pager = new PagerModel(search.PageIndex, search.PageSize, rowsCount);//定義Pager Model PagedListModel<Models.BlockEntity> dataList = new PagedListModel<Models.BlockEntity>() { Pager = pager, Data = blockList };//定義返回到 View 的 PagedListModel return View(dataList); } catch (Exception ex) { logger.Error(ex);//記錄日志 throw; }
頁面處理
首先在頁面頂部聲明 model 模型
@model PagedListModel<Models.BlockEntity>
在頁面上遍歷數據
@foreach (Models.BlockEntity item in Model.Data)
在需要添加分頁信息的地方使用 HtmlHelper 的 Pager 擴展方法
@Html.Pager(Model.Pager)
View代碼示例
1 @model PagedListModel<Models.BlockEntity> 2 <table class="table table-hover"> 3 <thead> 4 <tr> 5 <th>黑名單類型</th> 6 <th>黑名單內容</th> 7 <th>拉入黑名單時間</th> 8 <th>狀態</th> 9 <th>操作</th> 10 </tr> 11 </thead> 12 <tbody> 13 @foreach (Models.BlockEntity item in Model.Data) 14 { 15 string className = "bg-success"; 16 if (!item.IsActive) 17 { 18 className = "bg-danger"; 19 } 20 <tr class="@className"> 21 <td> 22 @item.BlockType.TypeName 23 </td> 24 <td> 25 @item.BlockValue 26 </td> 27 <td> 28 @item.BlockTime 29 </td> 30 <td> 31 @if (item.IsActive) 32 { 33 <span>啟用</span> 34 } 35 else 36 { 37 <span>禁用</span> 38 } 39 </td> 40 <td> 41 @if (item.IsActive) 42 { 43 <button type="button" class='btn btn-warning' onclick="UpdateStatus('@item.BlockId', '@item.BlockValue',-1,this)">禁用</button> 44 } 45 else 46 { 47 <button type="button" class='btn btn-warning' onclick="UpdateStatus('@item.BlockId','@item.BlockValue',1,this)">啟用</button> 48 } 49 <button type="button" class="btn btn-danger" onclick="DeleteEntity('@item.BlockId','@item.BlockValue',this)">刪除</button> 50 </td> 51 </tr> 52 } 53 </tbody> 54 </table> 55 @Html.Pager(Model.Pager)
文章列表
全站熱搜