Search code examples
c#asp.net-mvcasp.net-mvc-5.2child-actions

Html.Action renders the partial menu inside with different link routes


I render a menu widget inside the header of a master layout.

_Layout.cshtml

@Html.Action("Menu", "Menu", new { area = "Application" })

FullMenu.cshtml

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li><a href='#'><span class='glyphicon glyphicon-education'></span></a></li>
        <li>@Html.ActionLink("Projects", "Index")</li>
        <li>@Html.ActionLink("Tests", "Index" })</li>
    </ul>
    @Html.Partial("_LoginPartial")
</div>

MenuController.cs

public class MenuController : Controller
{
    [ChildActionOnly]
    public ActionResult Menu()
    {
        string controller = (string) ControllerContext.ParentActionViewContext.RouteData.Values["controller"];
        if (controller == "Projects")
        {
            return PartialView("StartMenu");
        }
        else
            return PartialView("FullMenu");
    }
}

This is the rendered output in the browser

enter image description here

Why has the rendered hyperlinks a modified Controller name? Even more those links applied the Controller name of the widget itself => "Menu" Why is that?


Solution

  • You need to declare the controller too in the menu route

    @Html.ActionLink("Projects", "Index", "Projects")