源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[ASP.NET] 一个支持普通分页和综合分页的MVC分页Helper

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2016-9-18 11:27:11 | 显示全部楼层 |阅读模式
这篇文章主要为大家详细介绍了一个支持普通分页和综合分页的MVC分页Helper,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本人写的一个分页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
 {
 /// <summary>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <param name="htmlAttributes">分页头标签属性</param>
 /// <param name="className">分页样式</param>
 /// <param name="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>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <param name="className">分页样式</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>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</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>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <param name="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>
 /// 分页
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="id">分页id</param>
 /// <param name="currentPageIndex">当前页</param>
 /// <param name="pageSize">分页尺寸</param>
 /// <param name="recordCount">记录总数</param>
 /// <param name="className">分页样式</param>
 /// <param name="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>
 /// 获取普通分页
 /// </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("总共{0}条记录,共{1}页,当前第{2}页  ", recordCount, pageCount, currentPageIndex);
  if (currentPageIndex == 1)
  sb.Append("<span>首页</span> ");
  else
  {
  string url1 = string.Format(url.ToString(), 1);
  sb.AppendFormat("<span><a href={0}>首页</a></span> ", url1);
  }
  if (currentPageIndex > 1)
  {
  string url1 = string.Format(url.ToString(), currentPageIndex - 1);
  sb.AppendFormat("<span><a href={0}>上一页</a></span> ", url1);
  }
  else
  sb.Append("<span>上一页</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}>下一页</a></span> ", url1);
  }
  else
  sb.Append("<span>下一页</span> ");

  if (currentPageIndex == pageCount)
  sb.Append("<span>末页</span> ");
  else
  {
  string url1 = string.Format(url.ToString(), pageCount);
  sb.AppendFormat("<span><a href={0}>末页</a></span> ", url1);
  }
  return sb.ToString();
 }
 /// <summary>
 /// 获取数字分页
 /// </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>
 /// 分页模式
 /// </summary>
 public enum PageMode
 {
 /// <summary>
 /// 普通分页模式
 /// </summary>
 Normal,
 /// <summary>
 /// 普通分页加数字分页
 /// </summary>
 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<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; }
 }
}

使用示例:

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

回复

使用道具 举报

3

主题

2万

回帖

163

积分

注册会员

Rank: 2

积分
163
发表于 2022-8-28 10:29:45 | 显示全部楼层
谢谢分享,先下来用用
TS人妖演出表演服务q3268336102电话13168842816
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

67

积分

注册会员

Rank: 2

积分
67
发表于 2022-10-7 09:57:23 | 显示全部楼层
看到这帖子真是高兴!
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-12-26 20:23:32 | 显示全部楼层
好东西一定要看看!
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-2-17 06:31:40 | 显示全部楼层
先把创新班才能下班才能下班
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-3-9 11:00:50 | 显示全部楼层
撒旦撒旦撒擦擦擦擦
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

499

积分

中级会员

Rank: 3Rank: 3

积分
499
发表于 2023-5-16 22:59:11 | 显示全部楼层
2222222222222222
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

55

积分

注册会员

Rank: 2

积分
55
发表于 2023-10-16 15:50:26 | 显示全部楼层
很好,谢谢分享
回复 支持 反对

使用道具 举报

4

主题

2万

回帖

303

积分

中级会员

Rank: 3Rank: 3

积分
303
发表于 2023-11-25 13:46:06 | 显示全部楼层
建军节建军节建军节建军节
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2024-3-9 20:12:28 | 显示全部楼层
老衲笑纳了
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2024-11-29 16:05 , Processed in 0.073260 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表