3

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 ?

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.