tags:

views:

413

answers:

2

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 ?

+3  A: 

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.

Ole Lynge
+1  A: 

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.

dhulk