ayilmaz ayilmaz - 19 days ago 10
C# Question

Loading menu from a partial view in MVC

I am trying to seperate the menu from _Layout.cshtml but I am having difficulties.

My files are located like below.

Views/Home/Index
Views/Shared/_Layout
Views/Shared/_Menu


In _Layout.cshtml file, I have the code below...

@Html.Partial("_Menu")


Menu action is located in HomeController, and it looks like below

public ActionResult Menu()
{
MenuModel menu = new MenuModel();
return PartialView("_Menu", menu);
}


_Menu has the code below as first line

@model DomainModel.MenuModel


When I run the project on VS, everything looks perfect but I doesnt call Menu() action in HomeController. It somehow finds _Menu and displays it perfectly. But I dont understand why it doesnt call Menu() action?

Answer

@Html.Partial("_Menu") will just render HTML view, nothing to do with controller. If you want to call a controller use @{ Html.RenderAction("Menu", "Home"); }

Comments