Html.ActionLink and Html.RouteLink in MVC

The ActionLink method renders a hyperlink (anchor tag) to another controller action. Like the BeginForm helper you looked at earlier, the ActionLink helper uses the routing API under the hood to generate the URL. For example, when linking to an action in the same controller used to render the current view, you can simply specify the action name:

@Html.ActionLink("Link Text", "AnotherAction")

This produces the following markup, assuming the default routes:

<a href="/Home/AnotherAction">LinkText</a>

When you need a link pointing to an action of a different controller, you can specify the controller name as a third argument to ActionLink. For example, to link to the Index action of the ShoppingCartController, use the following code:

@Html.ActionLink("Link Text", "Index", "ShoppingCart")

Notice that you specify the controller name without the Controller suffix. You never specify the controller’s type name. The ActionLink methods have specifi c knowledge about ASP.NET MVC controllers and actions, and you’ve just seen how these helpers provide overloads enabling you to specify just the action name, or both the controller name and action name.
In many cases you’ll have more route parameters than the various overloads of ActionLink can handle. For example, you might need to pass an ID value in a route, or some other route parameter specifi c to your application. Obviously, the built-in ActionLink helper cannot provide overloads for these types of scenarios out of the box.
Fortunately, you can provide the helper with all the necessary route values using other overloads of ActionLink. One overload enables you to pass an object of type RouteValueDictionary. Another overload enables you to pass an object parameter (typically an anonymous type) for the routeValues parameter. The runtime refl ects over the properties of the object and uses them to construct route values (the property names will be the names of the route parameters, and the property values will represent the values of the route parameters). For example, to build a link to edit an album with an ID of 10720 you can use the following code:

@Html.ActionLink("Edit link text", "Edit", "StoreManager", new {id=10720}, null)

The last parameter in the preceding overload is the htmlAttributes argument. You saw earlier in the chapter how you can use this parameter to set any attribute value on an HTML element. The preceding code is passing a null (effectively not setting any additional attributes in the HTML). Even though the code isn’t setting attributes, you have to pass the parameter to invoke the correct overload of ActionLink.
The RouteLink helper follows the same pattern as the ActionLink helper, but also accepts a route name and does not have arguments for controller name and action name. For example, the fi rst example ActionLink shown previously is equivalent to the following:

@Html.RouteLink("Link Text", new {action="AnotherAction"})
Tagged . Bookmark the permalink.

Leave a Reply