using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace BookShopSys.Plus
{
/// <summary>
/// 使用扩展方法进行分页
/// </summary>
public static class HttpPages
{
public static MvcHtmlString Pager<T>(this HtmlHelper html, PageList<T> date)
{
//开始数字
int start = (date.PageIndex - 5) >= 1 ? (date.PageIndex - 5) : 1;
//结束数字
int end = (date.TotalPage - start) >= 10? start + 10 : (date.TotalPage - start);
//路由数据
var vs = html.ViewContext.RouteData.Values;
//URL数据
var queryString = html.ViewContext.HttpContext.Request.QueryString;
//合并URL数据
foreach (string key in queryString)
{
if (!string.IsNullOrEmpty(key)) vs[key] = queryString[key];
}
var formString = html.ViewContext.HttpContext.Request.QueryString;
foreach (string key in formString)
{
if (!string.IsNullOrEmpty(key)) vs[key] = formString[key];
}
var str = new StringBuilder();
var actionName = vs["action"].ToString();
//显示首页和上页
if (date.HasPreviousPage)
{
vs["pageIndex"] = 1;
str.AppendFormat("<li>{0}</li>", html.ActionLink("首页", actionName, vs));
vs["pageInde"] = date.PageIndex - 1;
str.AppendFormat("<li>{0}</li>", html.ActionLink("上页", actionName, vs));
}
//显示数字页码
for (int i = start; i < end; i++)
{
vs["pageIndex"] = i;
if (date.PageIndex == i)
{
str.AppendFormat("<li class='action'>{0}</li>", html.ActionLink(i.ToString(), actionName, vs));
}
else
{
str.AppendFormat("<li>{0}</li>", html.ActionLink(i.ToString(), actionName, vs));
}
}
//显示下页和尾页
if (date.HasNextPage)
{
vs["pageIndex"] = date.PageIndex + 1;
str.AppendFormat("<li>{0}</li>", html.ActionLink("下一页", actionName, vs));
vs["pageInde"] = date.TotalPage;
str.AppendFormat("<li>{0}</li>", html.ActionLink("尾页", actionName, vs));
}
return MvcHtmlString.Create(str.ToString());
}
}
}