MVC: Linking to Controller Action Methods

Using HTML links to execute controller action methods.

It’s common to have a requirement that a controller’s action method be called from several different pages within a site.
Also, this knowledge usually won’t be entirely flushed out at the beginning of project development.
To compensate for this need, MVC 3 provides a nice overloaded Html.ActionLink() that allows the developer to place a link calling any controller action desired.

To illustrate this function, I’ll begin with my Anonymous Types article where I illustrate the use of a new .NET 4.0 feature.
This article fully describes the code involved and resulting HTML you will see shortly.

Moving on from the details of Anonymous Types, I’ll illustrate how to use the Html.ActionLink() method.
This method takes as parameters (one of the overloaded version): text to displayed as a link, the name of the action method, and the controller name.
In this example, I’ll call my AnonymousTypes controller’s Index action method.

3-html-links-to-controller-actions

4-html-links-to-controller-actions

By clicking the homepage “Anonymous Types” link, I see the results of my work.

5-html-links-to-controller-actions

1-html-links-to-controller-actions

2-html-links-to-controller-actions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s