Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC Call another controller from view

Tags:

asp.net-mvc

Let say I'm on the page "Home/Index" and I want to go to the page MyOtherController/Index/1

How can I do this ?

I try :

<%= Html.ActionLink("Test", "Index", "MyOtherController", new { id=item.Id }) %>

Did I also have to add a route in Global.aspx file ?

like image 217
Melursus Avatar asked Jul 01 '26 00:07

Melursus


1 Answers

One option is to specify the name of the controller in the list of routevalues:

<%= Html.ActionLink("Test", "Index"
    , new { controller = "MyOtherController", id = item.Id }) %>

An alternative is to use the overload of ActionLink with htmlAttributes = null:

<%= Html.ActionLink("Test", "Index"
    , "MyOtherController", new { id = item.Id }, null) %>

The default route in the ASP.NET MVC template takes care of the routing in this case.

like image 65
Ole Lynge Avatar answered Jul 02 '26 14:07

Ole Lynge