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
<div id="Menu">
@{ List<WebApplicationMVC.Core.Models.menu> All = ViewBag.Menu;}
<ul>
@foreach (var One in All.Where(m => m.ParentId == 0))
{
List<WebApplicationMVC.Core.Models.menu> Child = All.Where(m => One.id == m.ParentId).ToList();
if (Child.Count > 0)
{
if(One.ActionLink == "Yes")
{
<li>
@Html.ActionLink(One.Name, One.Action, One.Controller)
@SubMenu(One, All)
</li>
}
else
{
<li>
@One.Name;
@SubMenu(One, All)
</li>
}
}
else
{
<li>@Html.ActionLink(One.Name, One.Action, One.Controller)</li>
}
}
</ul>
@helper SubMenu(WebApplicationMVC.Core.Models.menu Object, List<WebApplicationMVC.Core.Models.menu> List)
{
List<WebApplicationMVC.Core.Models.menu> subChilds = (from a in List where Object.id == a.ParentId select a).ToList();
<ul>
@foreach (var subOne in subChilds)
{
List<WebApplicationMVC.Core.Models.menu> InnerChilds = (from a in List where subOne.id == a.ParentId select a).ToList();
if (InnerChilds.Count > 0)
{
if (subOne.ActionLink == "Yes")
{
<li>
@Html.ActionLink(subOne.Name, subOne.Action, subOne.Controller)
@SubMenu(subOne, List)
</li>
}
else
{
<li>
@subOne.Name
@SubMenu(subOne, List)
</li>
}
}
else
{
<li>@Html.ActionLink(subOne.Name, subOne.Action, subOne.Controller)</li>
}
}
</ul>
}
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…