English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

A MVC Pagination Helper that Supports Both Normal Pagination and Comprehensive Pagination

This is a pagination Helper I wrote, supporting ordinary pagination (that is, home page, previous page, next page, last page, etc.) and comprehensive pagination (a combination of ordinary pagination and numeric pagination).

The following is}}Pagination effect:

Pagination 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>
 /// Pagination
 /// </summary>
 /// <param name="helper"></<param>
 /// <param name="id">Pagination ID</<param>
 /// <param name="currentPageIndex">Current page</<param>
 /// <param name="pageSize">Pagination size</<param>
 /// <param name="recordCount">Total number of records</<param>
 /// <param name="htmlAttributes">Pagination header tag attributes</<param>
 /// <param name="className">Pagination style</<param>
 /// <param name="mode">Pagination mode</<param>
 /// <returns></returns>
 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();
 }
 /// <summary>
 /// Pagination
 /// </summary>
 /// <param name="helper"></<param>
 /// <param name="id">Pagination ID</<param>
 /// <param name="currentPageIndex">Current page</<param>
 /// <param name="pageSize">Pagination size</<param>
 /// <param name="recordCount">Total number of records</<param>
 /// <param name="className">Pagination 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>
 /// Pagination
 /// </summary>
 /// <param name="helper"></<param>
 /// <param name="id">Pagination ID</<param>
 /// <param name="currentPageIndex">Current page</<param>
 /// <param name="pageSize">Pagination 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>
 /// Pagination
 /// </summary>
 /// <param name="helper"></<param>
 /// <param name="id">Pagination ID</<param>
 /// <param name="currentPageIndex">Current page</<param>
 /// <param name="pageSize">Pagination size</<param>
 /// <param name="recordCount">Total number of records</<param>
 /// <param name="mode">Pagination mode</<param>
 /// <returns></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>
 /// Pagination
 /// </summary>
 /// <param name="helper"></<param>
 /// <param name="id">Pagination ID</<param>
 /// <param name="currentPageIndex">Current page</<param>
 /// <param name="pageSize">Pagination size</<param>
 /// <param name="recordCount">Total number of records</<param>
 /// <param name="className">Pagination style</<param>
 /// <param name="mode">Pagination 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>
 /// 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 recordCount, PageMode mode)
 {
  int pageCount = (recordCount % pageSize == 0)63;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 Page {2}) pages   ", recordCount, pageCount, currentPageIndex);
  if (currentPageIndex == 1)
  sb.Append("<span>Home<"/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}>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>End Page</span> ");
  else
  {
  string url1 = string.Format(url.ToString(), pageCount);
  sb.AppendFormat("<span><a href={0}>Last Page</a></span> ", url1);
  }
  return sb.ToString();
 }
 /// <summary>
 /// Get numeric 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>
 /// Page Mode
 /// </summary>
 public enum PageMode
 {
 /// <summary>
 /// Normal pagination mode
 /// </summary>
 Normal,
 /// <summary>
 /// Normal pagination and numeric pagination
 /// </summary>
 Numeric
 }
} 

PagerQuery.cs contains two properties, one is the PageInfo entity class property Pager, which includes the RecordCount, CurrentPageIndex, and PageSize properties. The other is the Model EntityList property.

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; }
 }
}

Use Case:

@ 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">
 <h2>NewsList</h2>
 <table>
 <tr>
  <th></th>
  th>
  NoteID
  </th>
  th>
  Title
  </th>
  th>
  Author
  </th>
  th>
  Hit
  </th>
  th>
  ReplyNum
  </th>
 </tr>
 <% foreach (var item in Model.EntityList) { %>
 <tr>
  <td>
  <%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
  <%= Html.ActionLink("Details", "NewsDetail", new { noteID=item.NoteID })%>
  </td>
  <td>
  <%= Html.Encode(item.NoteID) %>
  </td>
  <td>
  <%= Html.Encode(item.Title) %>
  </td>
  <td>
  <%= Html.Encode(item.Author)%>
  </td>
  <td>
  <%= Html.Encode(item.Hit)%>
  </td>
  <td>
  <%= Html.Encode(item.ReplyNum)%>
  </td>
 </tr>
 <% } %>
 </table>
 <p>
 <%=Html.Pager("pager",Model.Pager.CurrentPageIndex,Model.Pager.PageSize,Model.Pager.RecordCount,PageMode.Numeric) %>
 </p>
</asp:Content>

 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.net).rar

That's all for this article. I hope it will be helpful to everyone's study and also hope everyone will support the Yana Tutorial.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal responsibility. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.

You May Also Like