i want to display dynamic menu in mvc layout page.
in my database table has menuid and parentid from that i want to display nested menus.
if anyone have solution please help me and if any other method for this give an example.
here is my database
Database Table Structure
here is my controller code
public ActionResult Index()
{
using (MachineShopDBEntities db = new MachineShopDBEntities())
{
List<MenuMaster> list = db.MenuMasters.ToList();
ViewBag.MenuList = new SelectList(list);
}
return View();
}
here is my model
public partial class MenuMaster
{
public int MenuID { get; set; }
public string MenuText { get; set; }
public string Description { get; set; }
public Nullable<int> ParentID { get; set; }
public string ControllerName { get; set; }
public string ActionName { get; set; }
public bool IsChecked { get; set; }
public List<MenuMaster> menus { get; set; }
public IEnumerable<SelectListItem> users { get; set; }
}
here is my view
<ul class="sidebar-menu">
@{
if (ViewBag.MenuList != null)
{
foreach (var items in ViewBag.MenuList.Items)
{
string action = items.ActionName;
string controller = items.ControllerName;
<li class="treeview">
@if (items.ParentID == items.MenuID)
{
<ul class="treeview-menu">
<li class="treeview">
<a href="/@items.ControllerName/@items.ActionName">
<i class="fa fa-angle-double-right"></i> <span>@items.MenuText</span>
<i class="fa fa-angle-left pull-right"></i>
</a>
</li>
</ul>
}
<a href="/@items.ControllerName/@items.ActionName">
<i class="fa fa-user"></i> <span>@items.MenuText</span>
<i class="fa fa-angle-left pull-right"></i>
</a>
</li>
}
}
}
this type of output i want output image
