开发者

Two parameters in asp.net mvc route

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜