ASP.NET MVC Paging, saving current request parameters

I have a grid with contact information that I should be able to scroll through.

All plumbing is already in place , with one last detail. Paging is done using a simple p Querystring parameter, for example. www.myurl.com/grid?p=3 is the third page; The repository automatically retrieves the correct data, as well as the total number of items. The size of each page is determined somewhere else, and I do not need to worry about that in the query line.

However, I also support search, etc. Search in the search query is indicated 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 out the second page of results.

The problem that I am facing now, since q (or other) parameters can be present in the query string, how do I create a pager for this (which should preserve the original parameters, so if I press the "2" button I need 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?

+3
source share
3 answers

I asked a similar question yesterday. You might want to check Save data to .net mvc

Below is the code copied from the Steve Sanderson 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();
    }
}
+1
source

, , URL-, "". , , , , "" .

, , , , . , 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}" });

, "" . , , , , .

ASIDE: [], ( ) OnActionExecuted. , , .

0

. , , , ( ) , , , , , - , . , , 1.

0
source

Source: https://habr.com/ru/post/1717785/


All Articles