ASP.NET MVC > URLs

Create hyperlink based on action method in ASP.NET MVC

How to create a hyperlink based on Controller action method?


To create a hyperlink based on controller action method, we can use Html.ActionLink helper methods.

@Html.ActionLink("Home using Html.Action", "Index", "Home")

In the above ActionLink method has three parameters

  • “Home using Html.Action” - is the text of the hyperlink
  • “Index” – is the action method of the controller
  • “Home” – is the controller name
POINT OF INTEREST

We can also use different overload methods of the Html.ActionLink to format our hyperlink or apply css styles or pass object parameters as we have passed in the Url.Action method previously.

@Html.ActionLink("Home using Html.Action", "Index", new { controller = "Home" }, new
{ @class = "myCSSCalss", style = "color:red;" })

Above ActionLink method will create a hyperlink with class attribute as “myCSSClass” and style as “color:red”. Notice that the 3rd parameter is explicitly mentioning controller parameter as “Home” as in this overload method the 3rd parameter is the object parameter.

To get only url of the action method, use Url.Action method.

 Views: 25325 | Post Order: 74



Write for us






Hosting Recommendations