In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to write a MVC pagination Helper that supports general paging and integrated paging. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Here is the paging effect:
Paging code:
PagerHelper.cs
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 {/ paging / paging id / / current page / paging size / Total number of records / paging header tag attributes / paging style / / paging mode / public static string Pager (this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, 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 () } / paging / paging id / / current page / / paging size / Total number of records / paging style / public static string Pager (this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, string className) {return Pager (helper, id, currentPageIndex, pageSize, recordCount, null, className,PageMode.Normal) } / paging / paging id / / current page / paging size / Total number of records / public static string Pager (this HtmlHelper helper,string id,int currentPageIndex,int pageSize,int recordCount) {return Pager (helper, id, currentPageIndex, pageSize, recordCount,null) } / paging / paging id / / current page / / paging size / Total number of records / paging mode / public static string Pager (this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,PageMode mode) {return Pager (helper, id, currentPageIndex, pageSize, recordCount, null,mode) } / paging / paging id / / current page / paging size / Total number of records / paging style / paging mode / public static string Pager (this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount,string className, PageMode mode) {return Pager (helper, id, currentPageIndex, pageSize, recordCount, null,className,mode) } / private static string GetNormalPage (int currentPageIndex, int pageSize, int recordCount,PageMode mode) {int pageCount = (recordCount%pageSize = = 0?recordCount/pageSize:recordCount/pageSize+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() != "page") url.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]); } StringBuilder sb = new StringBuilder(); sb.Append(""); sb.AppendFormat("总共{0}条记录,共{1}页,当前第{2}页 ", recordCount, pageCount, currentPageIndex); if (currentPageIndex == 1) sb.Append("首页 "); else { string url1 = string.Format(url.ToString(), 1); sb.AppendFormat("首页 ", url1); } if (currentPageIndex >1) {string url1 = string.Format (url.ToString (), currentPageIndex-1); sb.AppendFormat (previous page, url1);} else sb.Append (previous page); 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 (next page, url1) } else sb.Append ("next page"); if (currentPageIndex = = pageCount) sb.Append ("last page"); else {string url1 = string.Format (url.ToString (), pageCount); sb.AppendFormat ("last page", url1);} return sb.ToString () } / private 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) {Kmurf; m = 10 } else m = pageCount;} else m = 10; for (int I = k * 10 + 1; i NewsList NewsList NoteID Title Author Hit ReplyNum |
Controler:
[AcceptVerbs (HttpVerbs.Get)] public ActionResult NewsList (int boardID,int? Page) {PagerInfo pager = new PagerInfo (); NewsArticleInfo info = new NewsArticleInfo (); info.NewsBoard = new NewsBoardInfo (); info.NewsBoard.BoardID = boardID; pager.RecordCount = Resolve (). GetArticleDataList (info, ArticleTypeEnum.Pass); pager.PageSize = 10; pager.CurrentPageIndex = (pagebrush null? (int) page:1); IList result = Resolve (). GetArticleDataList (pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info); PagerQuery query = new PagerQuery (pager,result); return View (query);}
Source code download: http://xiazai.jb51.net/201609/yuanma/MvcPager(jb51.net).rar
After reading the above, do you have any further understanding of how to write a MVC pagination Helper that supports normal and integrated paging? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.