public static MvcHtmlString AjaxPaging(this HtmlHelper hh, Pager pager, string actionName, string controllerName, AjaxPagingOption option, RouteValueDictionary routeValues) { if (option == null) { option = new AjaxPagingOption(); } if (routeValues == null) { routeValues = new RouteValueDictionary(); } TagBuilder div = new TagBuilder("div"); div.MergeAttribute("class", "pager"); StringBuilder sb = new StringBuilder(); routeValues["page"] = pager.Cur; string hidRefreshUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, hh.RouteCollection, hh.ViewContext.RequestContext, true); sb.AppendLine(string.Format("<input type=\"hidden\" id=\"hid_RefreshUrl\" value=\"{0}\" >", hidRefreshUrl)); if (pager.Pag > 1)//显示页码 { if (pager.Cur > 1) { routeValues["page"] = pager.Cur - 1; string pageUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, hh.RouteCollection, hh.ViewContext.RequestContext, true); TagBuilder parent = new TagBuilder("a"); parent.InnerHtml = "<<"; parent.MergeAttribute("title", "上一页"); parent.MergeAttribute("href", option.Href); parent.MergeAttribute("onclick", option.ToString(pageUrl)); sb.AppendLine(parent.ToString()); } int showPage = 5; //showPage*2 int start = 1; int end = 5 * 2; if ((pager.Cur - showPage) < 1) { start = 1; } else { start = pager.Cur - showPage; } if ((pager.Cur + showPage) >= pager.Pag) { end = pager.Pag; } else { end = pager.Cur + showPage; } //首页 if (start > showPage) { routeValues["page"] = 1; string pageUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, hh.RouteCollection, hh.ViewContext.RequestContext, true); TagBuilder parent = new TagBuilder("a"); parent.InnerHtml = string.Format("<span>{0}</span>", 1); parent.MergeAttribute("title", "首页"); parent.MergeAttribute("href", option.Href); parent.MergeAttribute("onclick", option.ToString(pageUrl)); sb.AppendLine(parent.ToString()); } for (int i = start; i <= end; i++) { routeValues["page"] = i; string pageUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, hh.RouteCollection, hh.ViewContext.RequestContext, true); TagBuilder aLink = new TagBuilder("a"); if (i == pager.Cur) { aLink.MergeAttribute("class", "cur"); aLink.InnerHtml = string.Format(" <b>{0}</b> ", i); } else { aLink.InnerHtml = string.Format(" <span>{0}</span> ", i); } aLink.MergeAttribute("href", option.Href); aLink.MergeAttribute("onclick", option.ToString(pageUrl)); sb.AppendLine(aLink.ToString()); } if (end < pager.Pag - showPage) { routeValues["page"] = pager.Pag; string pageUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, hh.RouteCollection, hh.ViewContext.RequestContext, true); TagBuilder next = new TagBuilder("a"); next.InnerHtml = string.Format("<span>{0}</span>", pager.Pag); next.MergeAttribute("title", "尾页"); next.MergeAttribute("href", option.Href); next.MergeAttribute("onclick", option.ToString(pageUrl)); sb.AppendLine(next.ToString()); } if (pager.Cur < pager.Pag) { routeValues["page"] = pager.Cur + 1; string pageUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, hh.RouteCollection, hh.ViewContext.RequestContext, true); TagBuilder next = new TagBuilder("a"); next.InnerHtml = ">>"; next.MergeAttribute("title", "下一页"); next.MergeAttribute("href", option.Href); next.MergeAttribute("onclick", option.ToString(pageUrl)); sb.AppendLine(next.ToString()); } } div.InnerHtml = sb.ToString(); return MvcHtmlString.Create(div.ToString()); }
public static MvcHtmlString AjaxPaging(this HtmlHelper hh, Pager pager, string actionName, string controllerName, AjaxPagingOption option, Object routeValues) { return AjaxPaging(hh, pager, actionName, controllerName, option, new RouteValueDictionary(routeValues)); }