This article is mainly for you to introduce the MVC htmlhelper extension, the realization of paging function, with a certain reference value, interested in small partners can refer to
The MVC HtmlHelper extension class Paginghelper realizes the paging function, for everybody reference, the concrete content is as follows
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{//< Summary>///pagination class if a page displays two lists only need to copy the class to the project rename one can be///</summary> public static class Paginghelper {#region Property prop Erty//<summary>///Current page//</summary> private static int? _currentpage = null; <summary>///Current page///</summary> public static int CurrentPage {get {return _currentpage?? 1; } set {_currentpage = value; }}////<summary>//per page record number///</summary> private static int? _pagesize = null; <summary>///per page number of records////</summary> public static int PageSize {get {return _pagesize?? 15; } set {_pagesize = value; }}////<summary> whether to display previous page///</summary> public static bool Haspreviouspage {get {return (C UrrentpAge > 1); }}////<summary> whether to display the next page///</summary> public static bool Hasnextpage {get {return (Curre Ntpage < totalpages); }}///<summary>//Current page:///</summary> public static string Currentpagedisplayname {get; set;} <summary>//Page display:///</summary> 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;} <summary>/////</summary> 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); }}///<summary>//Set Paging URL Eg:/admin/product/index///</summary> public static string Pagingurl { Get Set }///<summary>///default page, set paging parameter name Eg:/admin/product/index? pagingparamname=1//</summary> public static string Pagingparamname {get; Set } #endregion #region Paging String//<summary>//MVC paging if using jquery paging only requires that the class does not need an HREF to implement://$ (". Class value"). L Ive ("click", Function () {///var page = $ (this). attr ("Pagingparamname value"); $ ("#order"). HTML (""). Load ("/customer/order?page=" +page); }); Live automatically adds events to traversal///</summary>//<param name= "HTML" ></param>//<param name= "Htmlattribute S ">new {@class =" grey ", Pagingparamname=" page ", href="/admin/product/index "rel=" External nofollow "} Pagingparamname default page, anonymous class Add control properties </param>///<returns></returns> public static mvchtmlstring Paging (This System.Web.Mvc.HtmlHelper HTML, object htmlattributes) {Routevaluedictionary values = new RouteValueDictionary (htmlattributes); #region Property Assignment if (values["href"]! = null) {Pagingurl = values["href"]. ToString (); } if (values["Pagingparamname"]! = null) {Pagingparamname = values["Pagingparamname"]. ToString (); Values. Remove ("Pagingparamname"); } else {pagingparamname = "page"; } #endregion #region the outermost p/span tagbuilder builder = new Tagbuilder ("P");//span//create ID, note to set Idattributedotreplac first The Ement property is then executed before the Generateid method. Builder. Idattributedotreplacement = "_"; Builder. Generateid (ID); Builder. addCssClass (""); Builder. Mergeattributes (values); Builder. InnerHtml = Pagingbuilder (values); #endregion return Mvchtmlstring.create (builder. ToString (Tagrendermode.normal));//resolve to display HTML tag directly} private static string Pagingbuilder (RouteValueDictionary values) {# The region condition search includes additional parameters 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 Paging statistics sb. AppendFormat ("total {0} Records page {1} of {2}", TotalCount, CurrentPage, totalpages); #endregion #region Home prev sb. AppendFormat (Tagbuilder (values, 1, "first"); Sb. AppendFormat ("<a href={0}?page=1{1}>first</a>", url,urlparameter); if (haspreviouspage) {sb. AppendFormat (Tagbuilder (values, CurrentPage-1, "Prev")); Sb. AppendFormat ("<a href={0}?page={1}{2}>prev</a>", url, CurrentPage-1, urlparameter); #endregion #region Paging logic if (TotalPages >) {if (currentpage + 5) < TotalPages) {if (current Page > 5) {for (int i = CurrentPage-5; I <= currentpage + 5; i++) {sb. Append (Tagbuilder (values, I, i.tostring ())); }} else {for (int i = 1; i <=; 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 ()) + "to"); }} #endregion #region next last if (Hasnextpage) {sb. AppendFormat (Tagbuilder (values, CurrentPage + 1, "Next")); Sb. AppendFormat ("<a href={0}?page={1}{2}>next</a>", url, currentpage + 1, urlparameter); } sb. AppendFormat (Tagbuilder (values, TotalPages, "last")); Sb. AppendFormat ("<a href={0}?page={1}{2}>last</a>", 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}}
Background Controller Code
GET:/home/public actionresult Index (int page) {page = page?? 1; Paginghelper.currentpage = Convert.ToInt32 (page); Paginghelper.pagesize = 20; {Gets the number of bars in the dataset, and the paging dataset} Paginghelper.totalcount = 2000; return View ();}
Front page code
@{viewbag.title = "Index";} @using htmlhelpermvc.models;
Eventually:
"Recommended"
1. ASP. NET free Video Tutorials
2. Geek Academy ASP. NET Video Tutorial
3. ASP. NET Video Tutorials