1.先自定义一个类为PagingInfo
using System;
using System.Collections.Generic; using System.Linq; using System.Web;namespace eBuul.ChinaJoy.BLL.Models
{ public class PagingInfo { /// <summary> /// 总条数 /// </summary> public int TotalItems { get; set; } /// <summary> /// 每页条数 /// </summary> public int ItemsPerPage { get; set; } /// <summary> /// 当前页数 /// </summary> public int CurrentPage { get; set; } /// <summary> /// 总页数 /// </summary> public int TotalPages { get { if (ItemsPerPage!=0) { return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage); } return 0; } } } }2.在Action中写入以下代码:
PagingInfo pagingInfo = new PagingInfo() { CurrentPage = pageIndex, ItemsPerPage = pageSize, TotalItems = total };
ViewBag.PagingInfo = pagingInfo;
3.在view视图中调用
<div class="pages">
@Html.PageLinkes("FineGame", "Index", (eBuul.ChinaJoy.BLL.Models.PagingInfo)ViewBag.PagingInfo, new string[] { "Keyword" }) </div>4.Html扩展方法实现
using System;
using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Text; using eBuul.ChinaJoy.BLL.Models;namespace System.Web.Mvc.Html { public static class PagingHelpers { public static MvcHtmlString PageLinkes(this HtmlHelper html, string controller, string ActionName, PagingInfo pagingInfo, string[] paramArray) { HttpContext contenxt = HttpContext.Current; if (pagingInfo.TotalItems==0) { return MvcHtmlString.Create(""); } //设置分页显示的区间值 int IsNextPage = 1; int currentPage = (pagingInfo.CurrentPage - 1) / 5; int start = 1, end = 5; if (currentPage >= 1) { start = (currentPage * 5) + 1; end = (currentPage + 1) * 5; } StringBuilder result = new StringBuilder(); string tmp = ""; if (paramArray != null && paramArray.Length > 0) { for (int n = 0; n < paramArray.Length; n++) { if (contenxt.Request.Params.AllKeys.Contains(paramArray[n])) tmp += "&" + paramArray[n] + "=" + contenxt.Request.Params[paramArray[n]]; } }
//生成中间的分页数目
int i = 0; for (i = start; i <= end; i++) { if (i <= pagingInfo.TotalPages) { TagBuilder litag = new TagBuilder("li"); TagBuilder tag = new TagBuilder("a"); if (tmp != "") tag.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, i, tmp)); else tag.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, i)); tag.InnerHtml = i.ToString(); if (i == pagingInfo.CurrentPage) litag.AddCssClass("li_bg"); litag.InnerHtml = tag.ToString(); result.Append(litag.ToString()); IsNextPage++; } else break; } //设置分页显示的下一区间值的起始位置(即页面中的"..."按钮) if (pagingInfo.TotalPages > 5 && (i - 1) != pagingInfo.TotalPages) { TagBuilder litagDot = new TagBuilder("li"); TagBuilder tagDot = new TagBuilder("a"); if (tmp != "") tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, end + 1, tmp)); else tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, end + 1)); tagDot.InnerHtml = "..."; litagDot.InnerHtml = tagDot.ToString(); result.Append(litagDot.ToString()); } //设置分页显示的上一区间值的起始位置(即页面中的"..."按钮) if (currentPage >= 1) { TagBuilder litagDot = new TagBuilder("li"); TagBuilder tagDot = new TagBuilder("a"); if (tmp != "") tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, start - 1, tmp)); else tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, start - 1)); tagDot.InnerHtml = "..."; litagDot.InnerHtml = tagDot.ToString(); result.Insert(0, litagDot.ToString()); }//上一页
if (pagingInfo.TotalPages > 1) { TagBuilder litagPre = new TagBuilder("li"); TagBuilder tagPre = new TagBuilder("a"); //TagBuilder tagImgPre = new TagBuilder("img"); //tagImgPre.MergeAttribute("src", "/Content/imghome/left02.png");if (pagingInfo.CurrentPage == 1)
tagPre.MergeAttribute("href", "#"); else { if (tmp != "") tagPre.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, pagingInfo.CurrentPage - 1, tmp)); else tagPre.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, pagingInfo.CurrentPage - 1)); } //tagPre.InnerHtml = tagImgPre.ToString(); tagPre.InnerHtml = "上一页"; litagPre.InnerHtml = tagPre.ToString(); result.Insert(0, litagPre); } //下一页 if (pagingInfo.TotalPages > 1) { TagBuilder litagNext = new TagBuilder("li"); TagBuilder tagNext = new TagBuilder("a"); //TagBuilder tagImgNext = new TagBuilder("img"); //tagImgNext.MergeAttribute("src", "/Content/imghome/right02.png"); if (pagingInfo.CurrentPage == pagingInfo.TotalPages) tagNext.MergeAttribute("href", "#"); else { if (tmp != "") tagNext.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, pagingInfo.CurrentPage + 1, tmp)); else tagNext.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, pagingInfo.CurrentPage + 1)); } //tagNext.InnerHtml = tagImgNext.ToString(); tagNext.InnerHtml = "下一页"; litagNext.InnerHtml = tagNext.ToString(); result.Append(litagNext.ToString()); } //总页数 TagBuilder litagTotal = new TagBuilder("li"); TagBuilder spantag = new TagBuilder("span"); litagTotal.AddCssClass("li_none"); spantag.InnerHtml = pagingInfo.TotalItems.ToString(); string paramUrl = string.Format("/{0}/{1}{2}", controller, ActionName, string.IsNullOrWhiteSpace(tmp)?string.Empty:"?"+tmp.TrimStart('&')); string pageHtml = string.Format("共<span id=\"pageSum\">{0}</span>页 {1}条记录 到第<input type=\"text\" size=\"2\" id=\"jumpto\" name=\"jumpto\" class=\"inputpage\" title=\"指定页码\" value=\"{3}\">页<input type=\"button\" title=\"指定页码\" value=\"确定\" id=\"btnGO\" class=\"button_li\"><input type=\"hidden\" value=\"{2}\" id=\"hdnParamUrl\"", pagingInfo.TotalPages, spantag.ToString(), paramUrl, pagingInfo.TotalPages == pagingInfo.CurrentPage ? pagingInfo.CurrentPage : pagingInfo.CurrentPage+1); litagTotal.InnerHtml = pageHtml; result.Append(litagTotal.ToString());TagBuilder ultag = new TagBuilder("ul");
ultag.InnerHtml = result.ToString(); return MvcHtmlString.Create(ultag.ToString()); } } }