189 8069 5689

如何通过HtmlHelper中PagingHelper类实现分页方法-创新互联

这篇文章给大家分享的是有关如何通过HtmlHelper中PagingHelper类实现分页方法的内容。小编觉得挺实用的,因此分享给大家做个参考。一起跟随小编过来看看吧。

10年积累的网站建设、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有铁门关免费网站建设让你可以放心的选择与我们合作。

MVC HtmlHelper扩展类PagingHelper实现分页功能的具体内容如下

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace HtmlHelperMvc.Models
{
 /// 
 /// 分页类如果一个页面显示两个列表只需要复制该类到项目中重命名一个就可以
 /// 
 public static class PagingHelper
 {
  #region 属性Property
  /// 
  /// 当前页码
  /// 
  private static int? _currentPage = null;
  /// 
  /// 当前页码
  /// 
  public static int CurrentPage
  {
   get
   {
    return _currentPage ?? 1;
   }
   set
   {
    _currentPage = value;
   }
  }
  /// 
  /// 每页记录条数
  /// 
  private static int? _pageSize = null;
  /// 
  /// 每页记录条数
  /// 
  public static int PageSize
  {
   get
   {
    return _pageSize ?? 15;
   }
   set
   {
    _pageSize = value;
   }
  }
  /// 
  /// 是否显示上一页
  /// 
  public static bool HasPreviousPage
  {
   get
   {
    return (CurrentPage > 1);
   }
  }
  /// 
  /// 是否显示下一页
  /// 
  public static bool HasNextPage
  {
   get
   {
    return (CurrentPage < TotalPages);
   }
  }
  /// 
  /// 当前页:
  /// 
  public static string CurrentPageDisplayName { get; set; }
  /// 
  /// 每页显示:
  /// 
  public static string PageSizeDisplayName { get; set; }
  public static string FirstDisplayName { get; set; }
  public static string PreDisplayName { get; set; }
  public static string NextDisplayName { get; set; }
  public static string LastDisplayName { get; set; }
  public static string TotalCountDisplayName { get; set; }
  public static string TotalPagesDisplayName { get; set; }
  /// 
  /// 总条数
  /// 
  public static int TotalCount
  {
   get;
   set;
  }
  public static int TotalPages
  {
   get
   {
    return (int)Math.Ceiling(TotalCount / (double)PageSize);
    //return (TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1);
   }
  }
  /// 
  /// 设置分页url eg:/Admin/Product/Index
  /// 
  public static string PagingUrl
  {
   get;
   set;
  }
  /// 
  /// 默认page,设置分页参数名 eg:/Admin/Product/Index?PagingParamName=1
  /// 
  public static string PagingParamName
  {
   get;
   set;
  }
  #endregion
  #region Paging String
  /// 
  /// MVC分页 如果用jquery分页只需要class不需要href,用以下实现:
  /// $(".class值").live("click", function () {
  /// var page = $(this).attr("pagingParamName值");
  /// $("#order").html("").load("/Customer/Order?page="+page);
  /// });live自动给遍历增加事件
  /// 
  /// 
  /// new {@class="grey",pagingParamName="page",href="/Admin/Product/Index" rel="external nofollow" } pagingParamName默认page,匿名类添加控件属性
  /// 
  public static MvcHtmlString Paging(this System.Web.Mvc.HtmlHelper html, object htmlAttributes)
  {
   RouteValueDictionary values = new RouteValueDictionary(htmlAttributes);
   #region 属性赋值
   if (values["href"] != null)
   {
    PagingUrl = values["href"].ToString();
   }
   if (values["pagingParamName"] != null)
   {
    PagingParamName = values["pagingParamName"].ToString();
    values.Remove("pagingParamName");
   }
   else
   {
    PagingParamName = "page";
   }
   #endregion
   #region 分页最外层p/span
   TagBuilder builder = new TagBuilder("p");//span
   //创建Id,注意要先设置IdAttributeDotReplacement属性后再执行GenerateId方法. 
   //builder.IdAttributeDotReplacement = "_";
   //builder.GenerateId(id);
   //builder.AddCssClass("");
   //builder.MergeAttributes(values);
   builder.InnerHtml = PagingBuilder(values);
   #endregion
   return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));//解决直接显示html标记
  }
  private static string PagingBuilder(RouteValueDictionary values)
  {
   #region 条件搜索时包括其他参数
   StringBuilder urlParameter = new StringBuilder();
   NameValueCollection collection = HttpContext.Current.Request.QueryString;
   string[] keys = collection.AllKeys;
   for (int i = 0; i < keys.Length; i++)
   {
    if (keys[i].ToLower() != "page")
    {
     urlParameter.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]);
    }
   }
   #endregion
   //CurrentPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"] ?? "0");
   StringBuilder sb = new StringBuilder();
   #region 分页统计
   sb.AppendFormat("Total  {0}   Records Page  {1} of  {2}   ", TotalCount, CurrentPage, TotalPages);
   #endregion
   #region 首页 上一页
   sb.AppendFormat(TagBuilder(values, 1, " First"));
   //sb.AppendFormat("First ",url,urlParameter);
   if (HasPreviousPage)
   {
    sb.AppendFormat(TagBuilder(values, CurrentPage - 1, " Prev "));
    //sb.AppendFormat("Prev ", url, CurrentPage - 1, urlParameter);
   }
   #endregion
   #region 分页逻辑
   if (TotalPages > 10)
   {
    if ((CurrentPage + 5) < TotalPages)
    {
     if (CurrentPage > 5)
     {
      for (int i = CurrentPage - 5; i <= CurrentPage + 5; i++)
      {
       sb.Append(TagBuilder(values, i, i.ToString()));
      }
     }
     else
     {
      for (int i = 1; i <= 10; i++)
      {
       sb.Append(TagBuilder(values, i, i.ToString()));
      }
     }
     sb.Append("... ");
    }
    else
    {
     for (int i = CurrentPage - 10; i <= TotalPages; i++)
     {
      sb.Append(TagBuilder(values, i, i.ToString()));
     }
    }
   }
   else
   {
    for (int i = 1; i <= TotalPages; i++)
    {
     sb.Append(" " + TagBuilder(values, i, i.ToString()) + " ");
    }
   }
   #endregion
   #region 下一页 末页
   if (HasNextPage)
   {
    sb.AppendFormat(TagBuilder(values, CurrentPage + 1, "Next"));
    //sb.AppendFormat("Next ", url, CurrentPage + 1, urlParameter);
   }
   sb.AppendFormat(TagBuilder(values, TotalPages, "Last"));
   //sb.AppendFormat("Last",url,TotalPages,urlParameter);
   #endregion
   return sb.ToString();
  }
  private static string TagBuilder(RouteValueDictionary values, int i, string innerText)
  {
   values[PagingParamName] = i;
   TagBuilder tag = new TagBuilder("a");
   if (PagingUrl != null)
   {
    values["href"] = PagingUrl + "?" + PagingParamName + "= " + i + "   ";
   }
   if (CurrentPage == i && innerText != " First" && innerText != " Last")
   {
    values["id"] = "on";
   }
   else
   {
    tag.Attributes["id"] = "";
   }
   tag.MergeAttributes(values);
   tag.SetInnerText(innerText);
   return tag.ToString();
  }
  #endregion
 }
}

后台Controller代码

//
// GET: /Home/

public ActionResult Index(int? page)
{
 page = page ?? 1;
 PagingHelper.CurrentPage = Convert.ToInt32(page);
 PagingHelper.PageSize = 20;

 //{获取数据集的中条数,以及分页的数据集}

 PagingHelper.TotalCount = 2000;
 return View();
}

前台页面代码

@{
 ViewBag.Title = "Index";
}
@using HtmlHelperMvc.Models;

Index


@Html.Paging(new { @class = "active" })

最终效果图:

如何通过HtmlHelper中PagingHelper类实现分页方法

感谢各位的阅读!关于如何通过HtmlHelper中PagingHelper类实现分页方法就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:如何通过HtmlHelper中PagingHelper类实现分页方法-创新互联
网站路径:http://jkwzsj.com/article/essge.html

其他资讯