Melursus
Melursus

Reputation: 10578

Asp.Net MVC Call another controller from view

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 ?

Upvotes: 3

Views: 5184

Answers (2)

Blair Scott
Blair Scott

Reputation: 1885

I don't believe ActionLink has an overload matching that particular signature. You would need to add "null" after your route values to find a matching one (for htmlAttributes). Ole's solution would be cleaner though so it's really a matter of preference. It also will help with readability so you don't have to guess whether each parameter is link text, an action/controller, etc.

Upvotes: 1

Ole Lynge
Ole Lynge

Reputation: 4587

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.

Upvotes: 8

Related Questions