asp.net MVC split-page code sharing _ Practical Tips

Source: Internet
Author: User
Tags httpcontext prev static class

This example of this article for you to share the MVC pagination code for your reference, the details are as follows

Using System.Collections.Generic;
Using System.Collections.Specialized;
Using System.Linq;
Using System.Web;
Using System.Text;
Using SYSTEM.WEB.MVC;
Using System.Web.Routing;
Using System.Data.Objects.DataClasses;  namespace SYSTEM.WEB.MVC {public static class Pagerhelper {///<summary>///paging///</summary>/// <param name= "helper" ></param>///<param name= "id" > Paging id</param>///"<param name=" currentpa Geindex "> Current page </param>///<param name=" pageSize "> Paging size </param>///<param name=" RecordCount "& gt; Total records </param>///<param name= "htmlattributes" > Page Header label properties </param>///<param name= "ClassName" > Pagination Styles </param>///<param name= "mode" > Paging mode </param>///<returns></returns> Public St Atic string Pager (This HtmlHelper helper, string id, int currentpageindex, int pageSize, int recordCount, Object Htmlattri Butes, String className, Pagemode mode) {Tagbuilder buIlder = new Tagbuilder ("table"); Builder.
   Idattributedotreplacement = "_"; Builder.
   Generateid (ID); Builder.
   addCssClass (ClassName); Builder.
   Mergeattributes (New RouteValueDictionary (htmlattributes)); Builder.
   InnerHtml = Getnormalpage (CurrentPageIndex, pageSize, recordCount, mode); Return builder.
  ToString (); ///<summary>///Paging///</summary>///<param name= "helper" ></param>///<param na Me= "id" > Pagination id</param>///<param name= "CurrentPageIndex" > Current page </param>///<param name= " PageSize "> Paging size </param>///<param name=" RecordCount "> Records total </param>///<param name=" ClassName "> Pagination styles </param>///<returns></returns> public static string Pager (this HtmlHelper helper, string ID, int currentpageindex, int pageSize, int recordCount, string className) {return Pager (helper, ID, Currentpageinde
  X, pageSize, RecordCount, NULL, ClassName, pagemode.normal); }///<summary>///Paging///</summary>///<param name= "helper" ></param>///<param name= "IDs" > Points Page id</param>///<param name= "CurrentPageIndex" > Current page </param>///<param name= "pageSize" > Paging Size & lt;/param>///<param name= "RecordCount" > Total records </param>///<returns></returns> public Stat IC string Pager (This HtmlHelper helper, string id, int currentpageindex, int. pageSize, int recordCount) {return Pag

  ER (helper, id, CurrentPageIndex, pageSize, recordCount, NULL); ///<summary>///Paging///</summary>///<param name= "helper" ></param>///<param N Ame= "id" > Pagination id</param>///<param name= "CurrentPageIndex" > Current page </param>///<param name= " PageSize "> Paging size </param>///<param name=" RecordCount "> Records total </param>///<param name=" mode "> Paging mode </param>///<returns></returns> public static string Pager (this HtMlhelper Helper, string id, int currentpageindex, int pageSize, int recordCount, Pagemode mode) {return Pager (helper
  , IDs, CurrentPageIndex, pageSize, recordCount, null, mode); ///<summary>///Paging///</summary>///<param name= "helper" ></param>///<param na Me= "id" > Pagination id</param>///<param name= "CurrentPageIndex" > Current page </param>///<param name= " PageSize "> Paging size </param>///<param name=" RecordCount "> Records total </param>///<param name=" ClassName "> Pagination style </param>///<param name=" mode > Paging mode </param>///<returns></returns> public s Tatic string Pager (This HtmlHelper helper, string id, int currentpageindex, int pageSize, int recordCount, String Classnam
  E, Pagemode mode) {return Pager (helper, id, CurrentPageIndex, pageSize, RecordCount, NULL, className, mode); }///<summary>///get normal pagination///</summary>///<param name= "CurrentPageIndex" ></param>///<param name= "pageSize" ></param>///<param name= "RecordCount" ></param> <returns></returns> private static string getnormalpage (int currentpageindex, int pageSize, int record Count, Pagemode mode) {int pagecount = (recordCount% PageSize = = 0? recordcount/pagesize:recordcount/pagesi
   Ze + 1);
   StringBuilder url = new StringBuilder (); Url.
   Append (HttpContext.Current.Request.Url.AbsolutePath + "? Page={0}");
   NameValueCollection collection = HttpContext.Current.Request.QueryString; String[] Keys = collection.
   AllKeys; for (int i = 0; i < keys. Length; i++) {if (Keys[i]. ToLower ()!= the "page" URL.
   AppendFormat ("&{0}={1}", Keys[i], collection[keys[i]);
   StringBuilder sb = new StringBuilder (); Sb.
   Append ("<tr><td>"); Sb.
   AppendFormat ("total {0} records, total {1} pages, current {2} page", RecordCount, PageCount, CurrentPageIndex); if (CurrentPageIndex = = 1) sb. Append ("<span> home &LT;/span> "); else {string url1 = string. Format (URL.
    ToString (), 1); Sb.

   AppendFormat ("<span><a href={0}> home </a></span>", URL1); } if (CurrentPageIndex > 1) {string url1 = string. Format (URL.
    ToString (), currentPageIndex-1); Sb.
   AppendFormat ("<span><a href={0}> prev </a></span>", URL1); Else sb.

   Append ("<span> prev </span>"); if (mode = = Pagemode.numeric) sb. Append (Getnumericpage (CurrentPageIndex, PageSize, RecordCount, PageCount, url.)

   ToString ())); if (CurrentPageIndex < PageCount) {string url1 = string. Format (URL.

    ToString (), CurrentPageIndex + 1); Sb.

   AppendFormat ("<span><a href={0}> next Page </a></span>", URL1); Else sb.

 

   Append ("<span> next page </span>"); if (CurrentPageIndex = = PageCount) sb.

   Append ("<span> last </span>"); else {string url1 = string. Format (URL. ToString (), PagecoUNT); Sb.

   AppendFormat ("<span><a href={0}> last </a></span>", URL1); Return SB.

  ToString ();
  ///<summary>///Get digital paging///</summary>///<param name= "CurrentPageIndex" ></param> <param name= "PageSize" ></param>///<param name= "RecordCount" ></param>///<param Name = "PageCount" ></param>///<param name= "url" ></param>///<returns></returns> Privat e static string getnumericpage (int currentpageindex, int pageSize, int recordCount, int pagecount, string url) {int
   K = CURRENTPAGEINDEX/10;
   int m = currentpageindex% 10;
   StringBuilder sb = new StringBuilder ();
     if (CURRENTPAGEINDEX/10 = = PAGECOUNT/10) {if (M = = 0) {k--;
    m = 10;
   else m = pagecount% 10;
   else M = 10; for (int i = k * ten + 1; I <= K * + M; i++) {if (i = = CurrentPageIndex) sb. AppendFormat ("<sPan><font color=red><b>{0}</b></font></span> ", i); else {string url1 = string. Format (URL.
     ToString (), i); Sb.

    AppendFormat ("<span><a href={0}>{1}</a></span>", URL1, I); } return SB.
  ToString (); }///<summary>///paging mode///</summary> public enum Pagemode {///<summary>///normal paging mode//

 /</summary> Normal,///<summary>///normal paging plus digital paging///</summary> Numeric}

HTML code

<div id= "Pagenav" class= "Pageinator" >

  @Html. showpagenavigate ((int) viewdata["pageindex"], (int) Viewbag.pagesize, (int) viewbag.totalcount);

 </div>

Controller

Shopentities shop = new Shopentities (); Public ActionResult Index () {//iqueryable<tbl_order> order = (from a in Shop.tbl_order select a). by (A=>a.id). Skip (10).
   Take (10);
   viewdata[' order ' = order;

   return View (); int pageIndex = request["PageIndex"] = = null? 1:int.
   Parse (request["PageIndex"]); int pageSize = request["pageSize"] = = null? 10:int.
   Parse (request["pageSize"]);

   int totalcount = 0;
   Pass the paging parameter data to the foreground viewdata["pageIndex"] = PageIndex;
   viewdata["pageSize"] = pageSize;
   Viewbag.pagesize = pageSize; Total Bar Number totalcount = Shop.tbl_order.
   Count ();
   Viewbag.totalcount = TotalCount;
   Send the current page data to the foreground. Viewdata.model = db.
   Userinfo.tolist (); list<tbl_order> pp = shop.tbl_order//. by (U => u.id)//. Skip ((pageIndex-1) * pageSize)//. Take (pageSize).
   ToList (); Iqueryable<tbl_order> pp = Shop.tbl_order. by (U => u.id).
   Skip ((pageIndex-1) * pageSize)    .
   Take (pageSize);
  Return View (PP); 


 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.