ayilmaz ayilmaz - 1 year ago 62
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.


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


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 Source

@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"); }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download