ASP.NET MVC 分页,保留当前查询参数

发布于 2024-08-04 10:09:39 字数 622 浏览 3 评论 0 原文

我有一个包含联系信息的网格,我需要能够对其进行翻页。

所有管道都已经就位,还有最后一个细节。分页是通过一个简单的 p 查询字符串参数完成的,例如 www.myurl.com/grid?p=3 将是第三页;存储库会自动获取正确的数据以及项目的总数。每个页面的大小是在其他地方定义的,我不需要在查询字符串中担心这一点。

不过,我也支持搜索等。搜索的搜索词在我的查询字符串中表示为 q 。所以现在我可以有一个组合:www.myurl.com/grid?q=tom&p=2,它搜索“tom”并提取第二页结果。

我现在面临的问题是,由于 q(或其他)参数可能出现在查询字符串中,我如何为此创建一个寻呼机(这需要保留原始参数,所以如果我点击“第 2 页”,它需要从

  • www.myurl.com/grid?a=1&b=xyz&q=tom

  • www.myurl.com/grid?a=1&b=xyz&q=tom&p=2

我该怎么做?

I have a grid with contact information which I need to be able to page through.

All the plumbing is already in place, with one last detail. Paging is done through a simple p Querystring parameter, e.g. www.myurl.com/grid?p=3 would be the third page; the repository is automatically fetching the right data, and also the total count of items. The size of each page is defined somewhere else and I don't need to worry about that in the query string.

However, I support searching etc. as well. The search term searched for in denoted as q in my Querystring. So now I can have a combination: www.myurl.com/grid?q=tom&p=2 which searches for "tom" and pulls the second page of results.

The problem I face now, since the q (or other) parameters may be present in the query string, how do I create a pager for this (which would need to keep the original parameters, so if I click on "page 2" it needs to go from

  • www.myurl.com/grid?a=1&b=xyz&q=tom

    to

  • www.myurl.com/grid?a=1&b=xyz&q=tom&p=2

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵山崖 2024-08-11 10:09:39

我昨天问了类似的问题。也许您想查看
在 .net mvc 中保留数据

以下是从 Steve Sanderson 的 图书

public static class PagingHelpers
{
    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"); // Construct an <a> tag

            tag.MergeAttribute("href", pageUrl(i));
            tag.InnerHtml = i.ToString();
            if (i == currentPage)
                tag.AddCssClass("selected");


            result.AppendLine(tag.ToString());
        }
        return result.ToString();
    }
}

I asked a similar question yesterday. Maybe you want to check out
Preserve data in .net mvc

following is the code copied from Steve Sanderson's book

public static class PagingHelpers
{
    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"); // Construct an <a> tag

            tag.MergeAttribute("href", pageUrl(i));
            tag.InnerHtml = i.ToString();
            if (i == currentPage)
                tag.AddCssClass("selected");


            result.AppendLine(tag.ToString());
        }
        return result.ToString();
    }
}
胡渣熟男 2024-08-11 10:09:39

因此,您需要让页面链接将用户引导至相同的 URL,但具有不同的“页面”值。最明显的方法是让呈现页面链接的代码从当前请求中获取查询字符串,修改“page”值,并使用修改后的字符串呈现链接。

或者,这是我采用的方法,您可以为“列表”页面定义一个新路线,其中包括任何分页和排序值。这样它们就成为 URL 的一部分,而且也很容易在控制器中处理。

包含排序和分页值的示例可能如下所示:

routes.MapRoute(
    "List",
    "{controller}/List/{pageNumber}/{sortBy}/{sortOrder}/{pageSize}",
    new { action = "List", sortBy = "Id", sortOrder = "Asc", pageNumber = 1, pageSize = 10 },
    new { sortBy = @"[A-Za-z0-9+-]*", sortOrder = "Asc|Desc", pageNumber = @"\d{1,6}", pageSize = @"\d{1,6}" });

显然,您的“List”操作方法需要能够解释这些值并相应地处理数据。这可能不是你真正想要的,但我只是想把它扔掉。

旁白:我还编写了一个 [List] 操作过滤器属性,它从路由中获取这些值,并且(如果模型是一个集合)自动应用 OnActionExecuted 中的排序和分页。这样我所要做的就是检索数据并设置模型。

So you need to have your page links direct the user to the same URL but with a different "page" value. The most obvious way to do this is to have your code that renders a page link grab the query string from the current request, modify the "page" value, and render a link using the modified string.

Alternatively, and this is the approach I've taken, you can define a new route for your "list" pages, that includes any paging and sorting values. This way they're part of the URL but also very easy to handle in your controller.

An example that includes sorting and paging values might look something like this:

routes.MapRoute(
    "List",
    "{controller}/List/{pageNumber}/{sortBy}/{sortOrder}/{pageSize}",
    new { action = "List", sortBy = "Id", sortOrder = "Asc", pageNumber = 1, pageSize = 10 },
    new { sortBy = @"[A-Za-z0-9+-]*", sortOrder = "Asc|Desc", pageNumber = @"\d{1,6}", pageSize = @"\d{1,6}" });

Obviously your "List" action method needs to be able to interpret the values and handle the data accordingly. This may not be what your really looking for but I just thought I'd throw it out there.

ASIDE: I've also written a [List] action filter attribute that picks up these values from the route and (if the model is a collection) automatically applies the sorting and paging in OnActionExecuted. This way all my action has to do is retrieve the data and set the model.

Spring初心 2024-08-11 10:09:39

我在会话中保留每个表单的搜索值。在响应搜索或分页的方法中,我首先加载会话中的任何值,然后使用查询字符串或表单参数中的值覆盖它们(并在必要时设置会话值),具体取决于它是 get 还是 post 。这样我就不必担心分页代码(包括搜索条件)——它只是使用已经存储的内容。如果我想进行新的搜索,则搜索框中的值会发生变化,并使用筛选按钮执行搜索 - 从第 1 页的结果开始。

I persist the search values for each form in the session. In methods that respond to search or paging, I first load up any values from the session, then override them (and set the session values, if necessary) with values from the querystring or form parameters, depending on whether it is a get or post. This way I don't have to worry about the paging code including the search criteria -- it simply uses what is already stored. If I want to do a new search, then the values in the search boxes change and the search is performed using a filter button -- which starts with results from page 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文