Asp.Net MVC Call another controller from view

Say I'm on the Home / Index page, and I want to go to the MyOtherController / Index / 1 page

How can i do this?

I'm trying to:

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

Do I also need to add a route to the Global.aspx file?

+3
source share
2 answers

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

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

An alternative is to use overload ActionLinkwith htmlAttributes = null:

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

In this case, the default route in the ASP.NET MVC pattern takes care of routing.

+8
source

, ActionLink , . "null" , ( htmlAttributes). Ole , . , , , / ..

+1

Source: https://habr.com/ru/post/1707624/


All Articles