views:

40

answers:

2

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

My controller action:

public 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 ?

A: 

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

Charlino
A: 

Did you try this in the Html.PageLinks helper?

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

Note the order of the parameters.

eKek0