using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace PagerExtend
{
public static class HtmlHelperExtensions
{
#region 分页扩展 PageExtend
/// <summary>
/// 分页option属性
/// </summary>
public class MoPagerOption
{
/// <summary>
/// 当前页 必传
/// </summary>
public int CurrentPage { get; set; }
/// <summary>
/// 总条数 必传
/// </summary>
public int Total { get; set; }
/// <summary>
/// 分页记录数(每页条数 默认每页15条)
/// </summary>
public int PageSize { get; set; }
/// <summary>
/// 路由地址(格式如:/Controller/Action) 默认自动获取
/// </summary>
public string RouteUrl { get; set; }
/// <summary>
/// 样式 默认 bootstrap样式 1
/// </summary>
public int StyleNum { get; set; }
}
/// <summary>
/// 分页扩展方法
/// </summary>
/// <param name="helper">html试图</param>
/// <param name="option">分页属性</param>
/// <returns>html样式</returns>
public static MvcHtmlString PageExtend(this HtmlHelper helper, MoPagerOption option)
{
if (option.PageSize <= 0) { option.PageSize = 15; }
if (option.CurrentPage <= 0) { option.CurrentPage = 1; }
if (option.Total <= 0) { return MvcHtmlString.Empty; }
//总页数
var totalPage = option.Total / option.PageSize + (option.Total % option.PageSize > 0 ? 1 : 0);
if (totalPage <= 0) { return MvcHtmlString.Create("分页异常"); }
//当前路由地址
if (string.IsNullOrEmpty(option.RouteUrl))
{
option.RouteUrl = helper.ViewContext.HttpContext.Request.RawUrl;
if (!string.IsNullOrEmpty(option.RouteUrl))
{
var lastIndex = option.RouteUrl.LastIndexOf("/");
option.RouteUrl = option.RouteUrl.Substring(0, lastIndex);
}
}
option.RouteUrl = option.RouteUrl.TrimEnd('/');
//构造分页样式
var sbPage = new StringBuilder(string.Empty);
switch (option.StyleNum)
{
case 2:
{
break;
}
default:
{
#region 默认样式
sbPage.Append("<nav>");
sbPage.Append(" <ul class=\"pagination\">");
sbPage.AppendFormat(" <li><a href=\"{0}/{1}\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span></a></li>",
option.RouteUrl,
option.CurrentPage - 1 <= 0 ? 1 : option.CurrentPage - 1);
for (int i = 1; i <= totalPage; i++)
{
sbPage.AppendFormat(" <li {1}><a href=\"{2}/{0}\">{0}</a></li>",
i,
i == option.CurrentPage ? "class=\"active\"" : "",
option.RouteUrl);
}
sbPage.Append(" <li>");
sbPage.AppendFormat(" <a href=\"{0}/{1}\" aria-label=\"Next\">",
option.RouteUrl,
option.CurrentPage + 1 > totalPage ? option.CurrentPage : option.CurrentPage + 1);
sbPage.Append(" <span aria-hidden=\"true\">»</span>");
sbPage.Append(" </a>");
sbPage.Append(" </li>");
sbPage.Append(" </ul>");
sbPage.Append("</nav>");
#endregion
}
break;
}
return MvcHtmlString.Create(sbPage.ToString());
}
#endregion
}
}
@using PagerExtend
@model IEnumerable<XinSheng.Api.Controllers.MoAirticle>
<table>
Url:@ViewBag.Url
@foreach (var item in Model)
{
<tr>
<td>@item.Title</td>
<td>@item.Author</td>
<td>@item.CreateTime</td>
</tr>
}
</table>
@Html.PageExtend(ViewBag.PagerOption as HtmlHelperExtensions.MoPagerOption)
using PagerExtend;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace XinSheng.Api.Controllers
{
[Serializable]
public class MoAirticle
{
public string Title { get; set; }
public string Author { get; set; }
public DateTime CreateTime { get; set; }
}
public class HomeController : Controller
{
public ActionResult Index(int id)
{
ViewBag.Title = "测试 分页";
List<MoAirticle> moAirticles = new List<MoAirticle>();
for (int i = 1; i < 50; i++)
{
moAirticles.Add(new MoAirticle
{
Author = "神牛步行" + i,
CreateTime = DateTime.Now,
Title = "博客园之" + i
});
}
ViewBag.Url = Request.RawUrl;
//初始化分页基础信息
var option = new HtmlHelperExtensions.MoPagerOption
{
CurrentPage = id,
PageSize = 15,
Total = moAirticles.Count
};
//动态传递分页属性
ViewBag.PagerOption = option;
var articles = moAirticles.Skip((option.CurrentPage - 1) * option.PageSize).Take(option.PageSize).ToList();
return View(articles);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有