Radek Radek - 3 months ago 17
ASP.NET (C#) Question

asp.net dropdown navigation menu controller change issue

So I created dropdown menu in _Layout.cshtml.

<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"
role="button" aria-expanded="false"><span>Menu</span></a>
<ul class="dropdown-menu" role="menu">
<li>@Html.ActionLink("FirstAction", "Action", "Controller1")</li>
<li>@Html.ActionLink("Second", "second", "Controller2", new { wszystko = true })</li>
<li>@Html.ActionLink("Third", "Third", "Controller3")</li>
</ul>
</li>
</ul>


On every run I can only once choose position from the menu. When I try to pick different action, application didn't see I want to change Controller. When I want to change action, application always searches in controller I'm currently in.

For example: if I am in Controller1/Action and I want to go to Controller2/Second, program always search for Controller1/Second action result.

Before I created that menu, links work fine. Can somebody tell me why is it happening?

Answer

The only overload for ActionLink that takes four parameters is:

HtmlHelper.ActionLink(string linkText, string actionName, object routeValues, object htmlAttributes)

Note there is no controller name. You need to use

HtmlHelper.ActionLink(string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)

For example

@Html.ActionLink("Second", "second", "Controller2", new { wszystko = true }, null)

Comments