国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

ASP.NET MVC4 HtmlHelper擴展類,實現分頁功能

2020-03-24 15:48:36
字體:
來源:轉載
供稿:網友
html' target='_blank'>public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount) var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath; pageSize = pageSize == 0 3 : pageSize; var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //總頁數 var output = new StringBuilder(); if (totalPages 1) output.AppendFormat(" a href='{0} pageIndex=1&pageSize={1}' 首頁 /a ", redirectTo, pageSize); if (currentPage 1) {//處理上一頁的連接 output.AppendFormat(" a href='{0} pageIndex={1}&pageSize={2}' 上一頁 /a ", redirectTo, currentPage - 1, pageSize); output.Append(" "); int currint = 5; for (int i = 0; i = 10; i++) {//一共最多顯示10個頁碼,前面5個,后面5個 if ((currentPage + i - currint) = 1 && (currentPage + i - currint) = totalPages) if (currint == i) {//當前頁處理 output.AppendFormat(" a href='{0} pageIndex={1}&pageSize={2}' {3} /a ", redirectTo, currentPage, pageSize, currentPage); else {//一般頁處理 output.AppendFormat(" a href='{0} pageIndex={1}&pageSize={2}' {3} /a ", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint); output.Append(" "); if (currentPage totalPages) {//處理下一頁的鏈接 output.AppendFormat(" a href='{0} pageIndex={1}&pageSize={2}' 下一頁 /a ", redirectTo, currentPage + 1, pageSize); output.Append(" "); if (currentPage != totalPages) output.AppendFormat(" a href='{0} pageIndex={1}&pageSize={2}' 末頁 /a ", redirectTo, totalPages, pageSize); output.Append(" "); output.AppendFormat(" label 第{0}頁 / 共{1}頁 /label ", currentPage, totalPages);//這個統計加不加都行 return new HtmlString(output.ToString());}2、添加公共類PagerInfo,PageQuerypublic class PagerInfo public int RecordCount { get; set; } public int CurrentPageIndex { get; set; } public int PageSize { get; set; }
//從數據庫在取得數據,并返回總記錄數 var temp = newsSer.LoadPageEntities(c = true, c = c.id, false, pageSize1, pageIndex1, out count); PagerInfo pager = new PagerInfo(); pager.CurrentPageIndex = pageIndex1; pager.PageSize = pageSize1; pager.RecordCount = count; PagerQuery PagerInfo, IQueryable news query = new PagerQuery PagerInfo, IQueryable news (pager, temp); return View(query);}4、View里的部分代碼 tbody @foreach (var item in Model.EntityList) input name="ids[]" type="checkbox" value="" / /td @item.author /td @item.title /td @item.ctime /td @Html.ActionLink("編輯", "Edit", new { id = item.id }) | @Html.ActionLink("刪除", "Delete", new { id = item.id }) /td /tr @*分頁*@ tr class="" td colspan="5" align="center" span @Html.ShowPageNavigate(Model.Pager.CurrentPageIndex, Model.Pager.PageSize, Model.Pager.RecordCount) /span /td /tr /tbody 5、添加一些樣式.paginator font: 12px Arial, Helvetica, sans-serif; padding: 10px 20px 10px 0; margin: 0px auto;.paginator a border: solid 1px #ccc; color: #0063dc; cursor: pointer; text-decoration: none;.paginator a:visited padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;.paginator .cpb border: 1px solid #F50; font-weight: 700; color: #F50; background-color: #ffeee5;.paginator a:hover border: solid 1px #F50; color: #f60; text-decoration: none;.paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover float: left; height: 16px; line-height: 16px; min-width: 10px; _width: 10px; margin-right: 5px; text-align: center; white-space: nowrap; font-size: 12px; font-family: Arial,SimSun; padding: 0 3px;.paginator label display:block; float:left; }6.總結這個案例簡單實現了在MVC中快速分頁,其實很多開源的項目中都有相關的HtmlHepler的擴展函數,其中也不乏帶有分頁的擴展,例如著名的開源商城項目nopCommerce,其中有就一個HtmlExtensions.cs擴展類,里面就有關于分頁的擴展,人家寫的可是相當專業哦,有興趣的可以研究一下。更多編程語言

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延安市| 吉林省| 简阳市| 麟游县| 南乐县| 合川市| 遵化市| 汾西县| 霍林郭勒市| 连城县| 普兰店市| 邯郸县| 泽州县| 永平县| 镇远县| 宜丰县| 马公市| 黎川县| 通化县| 那坡县| 巴里| 察雅县| 衡水市| 沐川县| 阜新| 北票市| 五河县| 潜山县| 永泰县| 玉屏| 视频| 鹤岗市| 句容市| 七台河市| 南召县| 普兰店市| 浦县| 桃园县| 禹城市| 阿坝| 巴彦淖尔市|