20

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.

Anthony Mastrean
  • 21,850
  • 21
  • 110
  • 188
gremo
  • 47,186
  • 75
  • 257
  • 421

5 Answers5

26

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
}) %>
LorenzCK
  • 7,411
  • 2
  • 36
  • 28
  • I get: SearchResults?search=RegistryManager.Models.PersonSearch&page=1 – gremo Nov 27 '09 at 19:41
  • 4
    Does not work...it passes the name of the class, not the key/values pairs corresponding to the object properties... – gremo Nov 27 '09 at 19:42
6

This blog post by Scott Guthrie helped me wrap my head around URL Routing: ASP.NET MVC Framework (Part 2): URL Routing

enter image description here enter image description here

I love that he included test cases! enter image description here

SavoryBytes
  • 35,571
  • 4
  • 52
  • 61
3

You need to override ToString().

2

If you are using Razor (I realize OP asked four years ago before Razor was invented, but people finding this maybe using it).

I was able to get something working by using an inline @helper method.

@helper RunnerLink(PersonSearch model, int page)
{
    var routeParms =new RouteValueDictionary(model.GetType().GetProperties().ToDictionary(p => p.Name, p => p.GetValue(model, null)));
    routeParms.Add("page", page.ToString());
    routeParms.Add("Controller", "Property");
    @Html.ActionLink("Search", "Index", routeParms)
}

Usage would be simple --

@RunnerLink(myPersonSearchInstance, 1)

It isn't the most elegant solution, but it works well if you want to pass an object in as a routeValue, but you need to pass additional items, such as Controller, Area or in OPs case page.

niico
  • 11,206
  • 23
  • 78
  • 161
Nate
  • 30,286
  • 23
  • 113
  • 184
1

You need use RouteLink instead ActionLink. Your code should look something like this

@Html.RouteLink("Next", new {controller = "SearchResults", action = "Index", search=samevalue, page=1 })