Rendering Helpers

Rendering helpers produce links to other resources inside an application, and can also enable you to build those reusable pieces of UI known as partial views.

Html.ActionLink and Html.RouteLink

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 specific 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 ...

Get Professional ASP.NET MVC 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.