I have created the N\'th level Dynamic Menu recursive method which creates a menu.
As seen in above picture the content is returned to ParialView named \"_M
Though War resolved my query above. But i still want to answer for people for whom the above answer seem complex. in most easiest possible way in MVC to populate Dynamic Navigation Menu from Database.
in Controller:
public ActionResult Index()
{
Menu menu_ = new Menu();
ViewBag.Menu = menu_.getMenuList();
return View();
}
in _layoutpage.Cshtml
@{ List All = ViewBag.Menu;}
@foreach (var One in All.Where(m => m.ParentId == 0))
{
List Child = All.Where(m => One.id == m.ParentId).ToList();
if (Child.Count > 0)
{
if(One.ActionLink == "Yes")
{
-
@Html.ActionLink(One.Name, One.Action, One.Controller)
@SubMenu(One, All)
}
else
{
-
@One.Name;
@SubMenu(One, All)
}
}
else
{
- @Html.ActionLink(One.Name, One.Action, One.Controller)
}
}
@helper SubMenu(WebApplicationMVC.Core.Models.menu Object, List List)
{
List subChilds = (from a in List where Object.id == a.ParentId select a).ToList();
@foreach (var subOne in subChilds)
{
List InnerChilds = (from a in List where subOne.id == a.ParentId select a).ToList();
if (InnerChilds.Count > 0)
{
if (subOne.ActionLink == "Yes")
{
-
@Html.ActionLink(subOne.Name, subOne.Action, subOne.Controller)
@SubMenu(subOne, List)
}
else
{
-
@subOne.Name
@SubMenu(subOne, List)
}
}
else
{
- @Html.ActionLink(subOne.Name, subOne.Action, subOne.Controller)
}
}
}