namespace System.Web.Mvc
{
public static class MikePagerHtmlExtensions
{
region MikePager 分页控件
public static string MikePager(this HtmlHelper html,PagedListdata)
{
string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
return MikePager(html,data,actioinName);
}
public static string MikePager(this HtmlHelper html,PagedListdata,object values)
{
string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
return MikePager(html,actioinName,values);
}
public static string MikePager(this HtmlHelper html,string action)
{
return MikePager(html,action,null);
}
public static string MikePager(this HtmlHelper html,string action,object values)
{
string controllerName = html.ViewContext.RouteData.GetRequiredString("controller");
return MikePager(html,controllerName,string controller,object values)
{
return MikePager(html,controller,new RouteValueDictionary(values));
}
public static string MikePager(this HtmlHelper html,RouteValueDictionary values)
{
string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
return MikePager(html,RouteValueDictionary values)
{
string controllerName = html.ViewContext.RouteData.GetRequiredString("controller");
return MikePager(html,RouteValueDictionary valuedic)
{
int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1;
int end = (data.TotalPage - start) > 9 ? start + 9 : data.TotalPage;
RouteValueDictionary vs = valuedic == null ? new RouteValueDictionary() : valuedic;
var builder = new StringBuilder();
builder.AppendFormat("
if (data.IsPreviousPage)
{
vs["pi"] = 1;
builder.Append(Html.LinkExtensions.ActionLink(html,"首页",vs,null));
builder.Append("");
vs["pi"] = data.PageIndex - 1;
builder.Append(Html.LinkExtensions.ActionLink(html,"上一页",null));
builder.Append("");
}
for (int i = start; i <= end; i++) //前后各显示5个数字页码
{
vs["pi"] = i;
if (i == data.PageIndex)
{
builder.Append("" + i.ToString() + "");
}
else
{
builder.Append("");
builder.Append(Html.LinkExtensions.ActionLink(html,i.ToString(),null));
}
}
if (data.IsNextPage)
{
builder.Append("");
vs["pi"] = data.PageIndex + 1;
builder.Append(Html.LinkExtensions.ActionLink(html,"下一页",null));
builder.Append("");
vs["pi"] = data.TotalPage;
builder.Append(Html.LinkExtensions.ActionLink(html,"末页",null));
}
builder.Append("每页" + data.PageSize + "条/共" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPage + "页