How do I create the correct route values for this ActionLink?

gremo picture gremo · Nov 27, 2009 · Viewed 85.3k times · Source

The Model of SearchResults.aspx is an instance of PersonSearch; when the request for a new page arrive (a GET request), the action method should take it and compute the new results.

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult SearchResults(PersonSearch search, int? page)
{
    ViewData["Results"] = new PaginatedList<Person>(_searchService.FindPersons(search), page ?? 0, 1);
    return View("SearchResults", search);
}

Then I have to generate the previous/next links:

<%= Html.ActionLink("Next Page >", "SearchResults", routeValues) %>

If I use routeValues = ViewData.Model I can see the object properties passed the address, but I can't add the "page" parameter.

Answer

LorenzCK picture LorenzCK · Nov 27, 2009

It think it would be better to create another object with the correct values, instead of using (and potentially altering the current routevalues):

<%=Html.ActionLink("Next Page >", "SearchResults", new {
    search = this.Model,
    page = 1 //or whatever
}) %>