這篇文章給大家介紹如何在MVC框架中使用HtmlHelper擴展類實現(xiàn)一個分頁功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
成都創(chuàng)新互聯(lián)公司長期為近1000家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為申扎企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),申扎網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。具體內(nèi)容如下
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace HtmlHelperMvc.Models { ////// 分頁類如果一個頁面顯示兩個列表只需要復(fù)制該類到項目中重命名一個就可以 /// public static class PagingHelper { #region 屬性Property ////// 當(dāng)前頁碼 /// private static int? _currentPage = null; ////// 當(dāng)前頁碼 /// public static int CurrentPage { get { return _currentPage ?? 1; } set { _currentPage = value; } } ////// 每頁記錄條數(shù) /// private static int? _pageSize = null; ////// 每頁記錄條數(shù) /// public static int PageSize { get { return _pageSize ?? 15; } set { _pageSize = value; } } ////// 是否顯示上一頁 /// public static bool HasPreviousPage { get { return (CurrentPage > 1); } } ////// 是否顯示下一頁 /// public static bool HasNextPage { get { return (CurrentPage < TotalPages); } } ////// 當(dāng)前頁: /// public static string CurrentPageDisplayName { get; set; } ////// 每頁顯示: /// public static string PageSizeDisplayName { get; set; } public static string FirstDisplayName { get; set; } public static string PreDisplayName { get; set; } public static string NextDisplayName { get; set; } public static string LastDisplayName { get; set; } public static string TotalCountDisplayName { get; set; } public static string TotalPagesDisplayName { get; set; } ////// 總條數(shù) /// public static int TotalCount { get; set; } public static int TotalPages { get { return (int)Math.Ceiling(TotalCount / (double)PageSize); //return (TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1); } } ////// 設(shè)置分頁url eg:/Admin/Product/Index /// public static string PagingUrl { get; set; } ////// 默認(rèn)page,設(shè)置分頁參數(shù)名 eg:/Admin/Product/Index?PagingParamName=1 /// public static string PagingParamName { get; set; } #endregion #region Paging String ////// MVC分頁 如果用jquery分頁只需要class不需要href,用以下實現(xiàn): /// $(".class值").live("click", function () { /// var page = $(this).attr("pagingParamName值"); /// $("#order").html("").load("/Customer/Order?page="+page); /// });live自動給遍歷增加事件 /// /// /// new {@class="grey",pagingParamName="page",href="/Admin/Product/Index" rel="external nofollow" } pagingParamName默認(rèn)page,匿名類添加控件屬性 ///public static MvcHtmlString Paging(this System.Web.Mvc.HtmlHelper html, object htmlAttributes) { RouteValueDictionary values = new RouteValueDictionary(htmlAttributes); #region 屬性賦值 if (values["href"] != null) { PagingUrl = values["href"].ToString(); } if (values["pagingParamName"] != null) { PagingParamName = values["pagingParamName"].ToString(); values.Remove("pagingParamName"); } else { PagingParamName = "page"; } #endregion #region 分頁最外層div/span TagBuilder builder = new TagBuilder("div");//span //創(chuàng)建Id,注意要先設(shè)置IdAttributeDotReplacement屬性后再執(zhí)行GenerateId方法. //builder.IdAttributeDotReplacement = "_"; //builder.GenerateId(id); //builder.AddCssClass(""); //builder.MergeAttributes(values); builder.InnerHtml = PagingBuilder(values); #endregion return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));//解決直接顯示html標(biāo)記 } private static string PagingBuilder(RouteValueDictionary values) { #region 條件搜索時包括其他參數(shù) StringBuilder urlParameter = new StringBuilder(); NameValueCollection collection = HttpContext.Current.Request.QueryString; string[] keys = collection.AllKeys; for (int i = 0; i < keys.Length; i++) { if (keys[i].ToLower() != "page") { urlParameter.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]); } } #endregion //CurrentPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"] ?? "0"); StringBuilder sb = new StringBuilder(); #region 分頁統(tǒng)計 sb.AppendFormat("Total {0} Records Page {1} of {2} ", TotalCount, CurrentPage, TotalPages); #endregion #region 首頁 上一頁 sb.AppendFormat(TagBuilder(values, 1, " First")); //sb.AppendFormat("First ",url,urlParameter); if (HasPreviousPage) { sb.AppendFormat(TagBuilder(values, CurrentPage - 1, " Prev ")); //sb.AppendFormat("Prev ", url, CurrentPage - 1, urlParameter); } #endregion #region 分頁邏輯 if (TotalPages > 10) { if ((CurrentPage + 5) < TotalPages) { if (CurrentPage > 5) { for (int i = CurrentPage - 5; i <= CurrentPage + 5; i++) { sb.Append(TagBuilder(values, i, i.ToString())); } } else { for (int i = 1; i <= 10; i++) { sb.Append(TagBuilder(values, i, i.ToString())); } } sb.Append("... "); } else { for (int i = CurrentPage - 10; i <= TotalPages; i++) { sb.Append(TagBuilder(values, i, i.ToString())); } } } else { for (int i = 1; i <= TotalPages; i++) { sb.Append(" " + TagBuilder(values, i, i.ToString()) + " "); } } #endregion #region 下一頁 末頁 if (HasNextPage) { sb.AppendFormat(TagBuilder(values, CurrentPage + 1, "Next")); //sb.AppendFormat("Next ", url, CurrentPage + 1, urlParameter); } sb.AppendFormat(TagBuilder(values, TotalPages, "Last")); //sb.AppendFormat("Last",url,TotalPages,urlParameter); #endregion return sb.ToString(); } private static string TagBuilder(RouteValueDictionary values, int i, string innerText) { values[PagingParamName] = i; TagBuilder tag = new TagBuilder("a"); if (PagingUrl != null) { values["href"] = PagingUrl + "?" + PagingParamName + "= " + i + " "; } if (CurrentPage == i && innerText != " First" && innerText != " Last") { values["id"] = "on"; } else { tag.Attributes["id"] = ""; } tag.MergeAttributes(values); tag.SetInnerText(innerText); return tag.ToString(); } #endregion } }
后臺Controller代碼
// // GET: /Home/ public ActionResult Index(int? page) { page = page ?? 1; PagingHelper.CurrentPage = Convert.ToInt32(page); PagingHelper.PageSize = 20; //{獲取數(shù)據(jù)集的中條數(shù),以及分頁的數(shù)據(jù)集} PagingHelper.TotalCount = 2000; return View(); }
前臺頁面代碼
@{ ViewBag.Title = "Index"; } @using HtmlHelperMvc.Models;Index
關(guān)于如何在MVC框架中使用HtmlHelper擴展類實現(xiàn)一個分頁功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。