分页助手 ASP.NET MVC
我已经实现了一个分页 HTML 帮助器(改编自 Steven Sanderson 的书)。这是当前的代码:
public static string PageLinks(this HtmlHelper html, int currentPage, int totalPages, Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= totalPages; i++)
{
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == currentPage)
tag.AddCssClass("selectedPage");
result.AppendLine(tag.ToString());
}
return result.ToString();
}
这会生成一堆指向我的项目的每个页面的链接。如果有很多页面,这可能会有点让人不知所措。我正在寻找一个类似的实现,它会产生一些不那么压倒性的东西,如下所示:
<previous ... 5 6 7 ... next>
其中 6 是当前页面。我确信在我必须重新实现轮子之前,一定有人已经实现了类似的东西?
I have implemented a paging HTML helper (adapted from Steven Sanderson's book). This is the current code:
public static string PageLinks(this HtmlHelper html, int currentPage, int totalPages, Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
for (int i = 1; i <= totalPages; i++)
{
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == currentPage)
tag.AddCssClass("selectedPage");
result.AppendLine(tag.ToString());
}
return result.ToString();
}
This produces a bunch of links to each page of my items. If there are many pages this can be a bit overwhelming. I am looking for a similar implementation which produces something less overwhelming like this:
<previous ... 5 6 7 ... next>
where 6 is the current page. I am sure someone must have implemented something similar, before I have to re-implement the wheel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论