Sunday, April 10, 2016

Asp.net MVC 2 Html.ActionLink example

Asp.Net MVC offers 2 ways of adding Hyperlinks in a page.

1. Using the Html.ActionLink method
2. Using the Url.Action method

In this post we shall see on how to add Hyperliks to a View page using Html.ActionLink. The Html.ActionLink method generates an  <a href=””> tag and places in the page.

The simplest form of Html.ActionLink, takes the Link Name and the Action Method name and the Controller name as parameters, if the controller name is not provided it takes the Home controller
The below line adds a link with text Register to the page which points to the Index Action method of the User controller.

<%= Html.ActionLink("Register","Index","User") %>

There are various overloaded versions of Html.ActionLink each of which provides a specific additional functionality.

A Simple Hyperlink

New User: <%= Html.ActionLink("Register","Index","User") %>

Hyperlink with Style class
<%= Html.ActionLink("Sign Up","Index","User",null,new {@class = "link"})%>

.link
{
      font: 10pt "Verdana";
      vertical-align: middle;
      color: Blue;
      text-align: left;
      text-decoration:underline;
      white-space: nowrap;
}

Search Flipkart Products:
Flipkart.com

No comments: