Mark Tabor

Mark Tabor

  • 587
  • 2k
  • 457.1k

MVc dynamic Menu using Partial View

May 12 2016 5:04 AM
I have made a simple Dynamic MVC menu application in which i have an entity model and i bind this entity model from database table {Menu table} , I have created a simple MVC application in which it automatically add home controller and i wrote the below c0de in home controller 
private TestEntities testEntities = new TestEntities();
public ActionResult Index()
{
ViewBag.MenuLevel1 = this.testEntities.Menus.Where(menu=>menu.ParentId==null ).ToList();
return View();
}
 
Secondly I have Menu Folder Under Views and in this Menu folder i have index view and my index view is just look like that
@model Menu.Models.Menu
<ul>
@foreach (var menuLevel1 in ViewBag.MenuLevel1)
{
<li>@menuLevel1.Name
@if (menuLevel1.Child.Count > 0)
{
<ul>
@foreach (var menuLevel2 in menuLevel1.Child)
{
<li>
@menuLevel2.Name
</li>
}
</ul>
}
</li>
}
</ul>
Now i want to make this Menu item as partial view and want to call it from _LAyout.cshtml how to do that ? i have check lot of example non of them makes me happy :) 

Answers (2)