One MVC page Helper that supports common pages and comprehensive pages, and one mvc page helper

Source: Internet
Author: User

One MVC page Helper that supports common pages and comprehensive pages, and one mvc page helper

The self-written Helper page supports common pages (that is, the home page, Previous Page, next page, last page, etc.) and Comprehensive pages (general pages and digital pages ).

Below isPaging 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 {/// <summary> /// paging // </summary> /// <param name = "helper"> </param>/ // <param name = "id"> page id </param> // <param name = "currentPageIn Dex "> current page </param> /// <param name =" pageSize "> page size </param> /// <param name =" recordCount "> total number of records </param> /// <param name = "htmlAttributes"> paging header label attributes </param> /// <param name = "className"> paging style </param>/ // <param name = "mode"> paging mode </param> // <returns> </returns> public static string Pager (this HtmlHelper helper, string id, int currentPageIndex, int pageSize, int recordCount, object htmlAttributes, string class Name, 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 = "helpe R "> </param> /// <param name =" id "> paging id </param> /// <param name =" currentPageIndex "> current page </param> /// <param name = "pageSize"> page size </param> /// <param name = "recordCount"> total number of records </param> /// <param name = "className"> paging style </param> // <returns> </returns> 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 );} /// <summary> /// paging // </summary> /// <param name = "helper"> </param> /// <param name =" id "> page id </param> /// <param name =" currentPageIndex "> current page </param> /// <param name =" pageSize "> page size </param> /// <param name = "recordCount"> total number of records </param> /// <returns> </returns> public static string Pager (this HtmlHelper helper, string id, int CurrentPageIndex, int pageSize, int recordCount) {return Pager (helper, id, currentPageIndex, pageSize, recordCount, null );} /// <summary> /// paging // </summary> /// <param name = "helper"> </param> /// <param name =" id "> page id </param> /// <param name =" currentPageIndex "> current page </param> /// <param name =" pageSize "> page size </param> /// <param name = "recordCount"> total number of records </param> /// <param name = "mode"> paging mode </param> /// <re Turns> </returns> 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 );} /// <summary> /// paging // </summary> /// <param name = "helper"> </param> /// <param name =" id "> page id </param> /// <param name =" currentPageIndex "> current page </param> /// <param name =" page Size "> page Size </param> /// <param name =" recordCount "> total number of records </param> /// <param name =" className "> page style </param> /// <param name = "mode"> paging mode </param> /// <returns> </returns> 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 );} /// <Summary> /// obtain the normal page /// </summary> /// <param name = "currentPageIndex"> </param> /// <param name = "pageSize"> </param> // <param name = "recordCount"> </param> // <returns> </returns> 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 ("<tr> <td>"); sb. appendFormat ("Total {0} records, total {1} pages, current {2} pages", recordCount, pageCount, currentPageIndex); if (currentPageIndex = 1) sb. append ("<span> homepage </span>"); else {string url1 = string. format (url. toString (), 1); sb. appendFormat ("<span> <a href = {0}> homepage </a> </span>", url1);} if (currentPageIndex> 1) {string url1 = string. format (url. toString (), currentPageIndex-1); sb. appendFormat ("<span> <a href = {0}> previous page </a> </span>", url1);} else sb. append ("<span> previous page </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 page </span>"); else {string url1 = string. format (url. toString (), pageCount); sb. appendFormat ("<span> <a href = {0}> last </a> </span>", url1);} return sb. toString ();} /// <summary> /// obtain the number pagination /// </summary> /// <param name = "currentPageIndex"> </param> /// <param name = "pageSize"> </param> // <param name = "recordCount"> </param> // <param name = "pageCount"> </param>/ // <param name = "url"> </param> // <returns> </returns> 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) {k --; m = 10;} else m = pageCount % 10 ;} else m = 10; for (int I = k * 10 + 1; I <= k * 10 + 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> /// add Numeric pagination to normal pagination /// </summary> Numeric }}

PagerQuery. cs contains two attributes: PageInfo object attributes Pager, including RecordCount, CurrentPageIndex, and PageSize. One is the Model EntityList attribute.

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace System.Web.Mvc{ public class PagerQuery<TPager,TEntityList> { public PagerQuery(TPager pager, TEntityList entityList) {  this.Pager = pager;  this.EntityList = entityList; } public TPager Pager { get; set; } public TEntityList EntityList { get; set; }  }}

PageInfo. cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace System.Web.Mvc{ public class PagerInfo { public int RecordCount { get; set; } public int CurrentPageIndex { get; set; } public int PageSize { get; set; } }}

Example:

@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PagerQuery<PagerInfo, IList<NewsArticleInfo>>>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> NewsList</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

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<INewsBLL>().GetArticleDataList(info, ArticleTypeEnum.Pass);  pager.PageSize = 10;  pager.CurrentPageIndex = (page!=null?(int)page:1);  IList<NewsArticleInfo> result = Resolve<INewsBLL>().GetArticleDataList(pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info);  PagerQuery<PagerInfo, IList<NewsArticleInfo>> query = new PagerQuery<PagerInfo, IList<NewsArticleInfo>>(pager,result);  return View(query); }

Source code download: http://xiazai.jb51.net/201609/yuanma/MvcPager (jb51.netw..rar

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.