Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 895
gremo Avatar asked Nov 27 '09 18:11

gremo


People also ask

What are route values?

Route values are the values extracted from a URL based on a given route template. Each route parameter in a template will have an associated route value and is stored as a string pair in a dictionary.

How do I pass model value through ActionLink?

Where RegLookup is a get ActionResult method in the vehicle controller which goes away and finds the model/make/colour information based on the registration field passed through to it. The Make/Model/Colour information is retrieved from a separate database.

How do you pass a route value in HTML BeginForm?

The value will only be added as query string values if its FormMethod. Get (and you can remove the route values from the BeginForm() method). But what is the point of this - they are hidden inputs, not editable values.

What is route value in MVC?

RouteData is a property of the base Controller class, so RouteData can be accessed in any controller. RouteData contains route information of a current request. You can get the controller, action or parameter information using RouteData as shown below.


2 Answers

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
}) %>
like image 159
LorenzCK Avatar answered Sep 22 '22 10:09

LorenzCK


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 hereenter image description here

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

like image 23
SavoryBytes Avatar answered Sep 25 '22 10:09

SavoryBytes