千家信息网

如何写一个支持普通分页和综合分页的MVC分页Helper

发表于:2025-01-16 作者:千家信息网编辑
千家信息网最后更新 2025年01月16日,今天就跟大家聊聊有关如何写一个支持普通分页和综合分页的MVC分页Helper,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。下面是分页效果:分页
千家信息网最后更新 2025年01月16日如何写一个支持普通分页和综合分页的MVC分页Helper

今天就跟大家聊聊有关如何写一个支持普通分页和综合分页的MVC分页Helper,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

下面是分页效果:

分页代码:

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 { ///  /// 分页 ///  ///  /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 /// 分页头标签属性 /// 分页样式 /// 分页模式 ///  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(); } ///  /// 分页 ///  ///  /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 /// 分页样式 ///  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); } ///  /// 分页 ///  ///  /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 ///  public static string Pager(this HtmlHelper helper,string id,int currentPageIndex,int pageSize,int recordCount) {  return Pager(helper, id, currentPageIndex, pageSize, recordCount,null); } ///  /// 分页 ///  ///  /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 /// 分页模式 ///  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); } ///  /// 分页 ///  ///  /// 分页id /// 当前页 /// 分页尺寸 /// 记录总数 /// 分页样式 /// 分页模式 ///  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("上一页 ", url1);  }  else  sb.Append("上一页 ");  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("下一页 ", url1);  }  else  sb.Append("下一页 ");  if (currentPageIndex == pageCount)  sb.Append("末页 ");  else  {  string url1 = string.Format(url.ToString(), pageCount);  sb.AppendFormat("末页 ", 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)  {   k--;   m = 10;  }  else   m = pageCount;  }  else  m = 10;  for (int i = k * 10 + 1; i <= k * 10 + m; i++)  {  if (i == currentPageIndex)   sb.AppendFormat("{0} ", i);  else  {   string url1 = string.Format(url.ToString(), i);   sb.AppendFormat("{1} ",url1, i);  }  }    return sb.ToString(); } } ///  /// 分页模式 ///  public enum PageMode { ///  /// 普通分页模式 ///  Normal, ///  /// 普通分页加数字分页 ///  Numeric }}

PagerQuery.cs包含两个属性,一个是PageInfo实体类属性Pager,包含RecordCount,CurrentPageIndex,PageSize三个属性。一个是Model EntityList属性。

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace System.Web.Mvc{ public class PagerQuery { 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; } }}

使用示例:

@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage>>" %> NewsList 

NewsList

<% foreach (var item in Model.EntityList) { %> <% } %>
NoteID Title Author Hit ReplyNum
<%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> | <%= Html.ActionLink("Details", "NewsDetail", new { noteID=item.NoteID })%> <%= Html.Encode(item.NoteID) %> <%= Html.Encode(item.Title) %> <%= Html.Encode(item.Author)%> <%= Html.Encode(item.Hit)%> <%= Html.Encode(item.ReplyNum)%>

<%=Html.Pager("pager",Model.Pager.CurrentPageIndex,Model.Pager.PageSize,Model.Pager.RecordCount,PageMode.Numeric) %>

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

源码下载:http://xiazai.jb51.net/201609/yuanma/MvcPager(jb51.net).rar

看完上述内容,你们对如何写一个支持普通分页和综合分页的MVC分页Helper有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

0