下面到了关键部分:
在文件PagerExtensions.cs底部再添加一个命名空间namespace System.Web.Mvc.Html。
在里面添加静态类public static class PagerExtensions。
在类中间添加函数public static MvcHtmlString Pager(……)
代码也很容易就是用UrlHelper.Action生成链接的地址,再创建一个StringBuilder _strBuilder,不停的向里面附加html代码,最后使用return MvcHtmlString.Create(_strBuilder.ToString());返回MvcHtmlString。就是写的太乱啦
public static MvcHtmlString Pager(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, PagerConfig pageConfig, string ctrlId, string cssClass, int digitalLinkNum, bool showTotalRecord, bool showCurrentPage, bool showTotalPage, bool showSelect, bool showInput) { UrlHelper _url = new UrlHelper(htmlHelper.ViewContext.RequestContext); StringBuilder _strBuilder = new StringBuilder("<div id=https://www.jb51.net/article/\"" + ctrlId + "https://www.jb51.net/article/\" class=https://www.jb51.net/article/\"" + cssClass + "https://www.jb51.net/article/\">"); if (showTotalRecord) _strBuilder.Append("共" + pageConfig.TotalRecord + pageConfig.RecordUnit + pageConfig.RecordName + " "); if (showCurrentPage) _strBuilder.Append("每页" + pageConfig.PageSize + pageConfig.RecordUnit + " "); if(showTotalPage) _strBuilder.Append("第" + pageConfig.CurrentPage + "页/共" + pageConfig.TotalPage + "页 "); //首页链接 if (pageConfig.CurrentPage > 1) { routeValues["page"] = 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbtn\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">首页</a>"); } else _strBuilder.Append("<span class=https://www.jb51.net/article/\"btn\">首页</span>"); //上一页 if (pageConfig.CurrentPage > 1) { routeValues["page"] = pageConfig.CurrentPage - 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbtn\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">上一页</a>"); } else _strBuilder.Append("<span class=https://www.jb51.net/article/\"btn\">上一页</span>"); //数字导航开始 int _startPage, _endPage; //总页数少于要显示的页数,页码全部显示 if (digitalLinkNum >= pageConfig.TotalPage) { _startPage = 1; _endPage = pageConfig.TotalPage; } else//显示指定数量的页码 { int _forward = (int)Math.Ceiling(digitalLinkNum / 2.0); if (pageConfig.CurrentPage > _forward)//起始页码大于1 { _endPage = pageConfig.CurrentPage + digitalLinkNum - _forward; if (_endPage > pageConfig.TotalPage)//结束页码大于总页码结束页码为最后一页 { _startPage = pageConfig.TotalPage - digitalLinkNum; _endPage = pageConfig.TotalPage; } else _startPage = pageConfig.CurrentPage - _forward; } else//起始页码从1开始 { _startPage = 1; _endPage = digitalLinkNum; } } //向上… if (_startPage > 1) { routeValues["page"] = _startPage - 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbatch\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">…</a>"); } //数字 for (int i = _startPage; i <= _endPage; i++) { if (i != pageConfig.CurrentPage) { routeValues["page"] = i; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linknum\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">" + i.ToString() + "</a>"); } else { _strBuilder.Append("<span>" + i.ToString() + "</span>"); } } //向下… if (_endPage < pageConfig.TotalPage) { routeValues["page"] = _endPage + 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbatch\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">…</a>"); } ////数字导航结束 //下一页和尾页 if (pageConfig.CurrentPage < pageConfig.TotalPage) { routeValues["page"] = pageConfig.CurrentPage + 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbtn\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">下一页</a>"); routeValues["page"] = pageConfig.TotalPage; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbtn\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">尾页</a>"); } else _strBuilder.Append("<span class=https://www.jb51.net/article/\"btn\">下一页</span><span class=https://www.jb51.net/article/\"btn\">尾页</span>"); //显示页码下拉框 if (showSelect) { routeValues["page"] = "-nspageselecturl-"; _strBuilder.Append(" 跳转到第<select id=https://www.jb51.net/article/\"nspagerselect\" data-url=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">"); for (int i = 1; i <= pageConfig.TotalPage; i++) { if (i == pageConfig.CurrentPage) _strBuilder.Append("<option selected=https://www.jb51.net/article/\"selected\" value=https://www.jb51.net/article/\"" + i + "https://www.jb51.net/article/\">" + i + "</option>"); else _strBuilder.Append("<option value=https://www.jb51.net/article/\"" + i + "https://www.jb51.net/article/\">" + i + "</option>"); } _strBuilder.Append("</select>页"); _strBuilder.Append("<script type=https://www.jb51.net/article/\"text/javascript\">$(\"#" + ctrlId + " #nspagerselect\").change(function () { location.href = $(\"#" + ctrlId + " #nspagerselect\").attr(\"data-url\").replace(\"-nspageselecturl-\", $(\"#" + ctrlId + " #nspagerselect\").val());});</script>"); } //显示页码输入框 if (showInput) { routeValues["page"] = "-nspagenumurl-"; _strBuilder.Append("转到第<input id=https://www.jb51.net/article/\"nspagernum\" type=https://www.jb51.net/article/\"text\" data-url=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\" />页"); _strBuilder.Append("<script type=https://www.jb51.net/article/\"text/javascript\">$(\"#" + ctrlId + " #nspagernum\").keydown(function (event) {if (event.keyCode == 13) location.href = $(\"#" + ctrlId + " #nspagernum\").attr(\"data-url\").replace(\"-nspagenumurl-\", $(\"#" + ctrlId + " #nspagernum\").val()); });</script>"); } _strBuilder.Append("</div>"); return MvcHtmlString.Create(_strBuilder.ToString()); }