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
@foreach (var menuLevel1 in ViewBag.MenuLevel1)
{
@if (menuLevel1.Child.Count > 0)
{
@foreach (var menuLevel2 in menuLevel1.Child)
{
@menuLevel2.Name
}
}
}
Mark TaborPosted Apr 3, 2019, 4:44 AM
Bhuvanesh MohankumarPosted May 12, 2016, 10:02 AM
Create a partial view and add the set of menu code to the partial view andcreate the view in the shared folder, which can be used any where in the applications.
Consume the partial view in the main viewwhere ever its required,
Reference: