开发者

Two parameters in asp.net mvc route

开发者 https://www.devze.com 2022-12-28 08:28 出处:网络
This is a modification to a question I\'ve asked before on this forum. My controller action: publi开发者_StackOverflowc ActionResult SearchResults(string searchTerm, int page)...

This is a modification to a question I've asked before on this forum.

My controller action:

publi开发者_StackOverflowc ActionResult SearchResults(string searchTerm, int page)...

My view:

<%= Html.PageLinks((int)ViewData["CurrentPage"], (int)ViewData["TotalPages"], i => Url.Action("SearchResults", new { page = i }))%>...

The route entries:

routes.MapRoute(
            null,
            "SearchResults",
            new { controller = "Search", action = "SearchResults", page = 1 } // Defaults
        );

        routes.MapRoute(
          "Search",
          "SearchResults/Page{page}",
          new { controller = "Search", action = "SearchResults" },
          new { page = @"\d+" }
        );

My goal is to have paging links for the search results. The problem is that when I click any page in the paging links, it gives me the search results of an empty serach term. How can I pass the search term parameter which is a string in addition to the page number parameter ? What should I put in the routing ?


I think if you add additional stuff into your route values dictionary that aren't defined in your route, it'll add them as get parameters.

So if you do something like:

Url.Action("SearchResults", new { page = i, searchterm = "YourSearchTerm" })

It should spit out a route like SearchResults/Page2?searchterm=yousearchterm

HTHs,
Charles


Did you try this in the Html.PageLinks helper?

Url.Action("SearchResults", new { searchTerm = "your query", page = i })

Note the order of the parameters.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号