【asp.net mvc】 扩展 htmlhelper 实现分页
参考文档:http://www.cnblogs.com/caofangsheng/p/5670071.html
http://www.cnblogs.com/artech/archive/2012/03/13/code-binding.html
http://www.cnblogs.com/xqaizx/p/5565447.html
【asp.net core】 扩展 TagHelper 分页 : http://www.cnblogs.com/wangrudong003/p/5705744.html
自定义分页类
using BooksStore.WebUI.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; namespace BooksStore.WebUI.HtmlHelpers { public static class PagingHelper { /// <summary> /// 创建分页html /// </summary> /// <param name="helper"></param> /// <param name="pagingInfo"></param> /// <param name="func"></param> /// <returns></returns> public static MvcHtmlString PageLinks(this HtmlHelper helper, PageInfo pagingInfo, Func<int, string> func) { var sb = new StringBuilder(); for (var i = 1; i <= pagingInfo.TotalPages; i++) { //创建 <a> 标签 var tagBuilder = new TagBuilder("a"); //添加特性 tagBuilder.MergeAttribute("href", func(i)); //添加值 tagBuilder.InnerHtml = i.ToString(); if (i == pagingInfo.PageIndex) { tagBuilder.AddCssClass("selected"); } sb.Append(tagBuilder); } return MvcHtmlString.Create(sb.ToString()); } } }
分页信息类
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace BooksStore.WebUI.Models { public class PageInfo { /// <summary> /// 总数 /// </summary> public int TotalItems { get; set; } /// <summary> /// 页容量 /// </summary> public int PageSize { get; set; } /// <summary> /// 当前页 /// </summary> public int PageIndex { get; set; } /// <summary> /// 总页数 /// </summary> public int TotalPages => (int)Math.Ceiling((decimal)TotalItems / PageSize); } }
修改 Views 文件夹下 web.config,新增命名空间
在页面调用方式
@Html.PageLinks(Model, x => Url.Action("Index", new { pageIndex = x , category = Model.CurrentCategory }))