Explaining
I am trying to make a dynamic menu by loading items from a database. I need 3 levels maximum inside the menu, for example:
<ul>
<li>Home</li>
<li>Peoples
<ul>
<li>Employee
<ul>
<li>Create</li>
<li>List</li>
<li>Edit</li>
</ul>
</li>
<li>Training</li>
<li>Material Requisition</li>
</ul>
</li>
</ul
Now, here is how I do today, but without success:
A partial view of "TopBar.cshtml" is shown on each page and is called inside "_Layout.cshtml" as follows:
_Layout.cshtml
<body>
@Html.Partial("TopBar")
<div class="container body-content">
@RenderBody()
(...)
and " TopBar.cshtml " shows the data using the code below
@model IEnumerable<SIGO.Models.TopMenu>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<div class="SigoLogo" onclick="location.href='@Url.Action("")'">
<a href="@Url.Action("Index", "Home")" title="Início">
<img src="~/Content/images/Wlogo.png" />
</a>
</div>
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"></button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
@if (Model != null){
foreach(var item in Model.Where(p => p.Nivel == 0)) {
if (Model.Where(s1 => s1.Parent == item.TopMenuID) != null) {
<li>@item.Descricao
<ul>
@foreach (var sub1 in Model.Where(s1 => s1.Parent == item.TopMenuID)) {
if (Model.Where(s2 => s2.Parent == sub1.TopMenuID) != null) {
<li>@sub1.Descricao
<ul>
@foreach (var sub2 in Model.Where(s2 => s2.Parent == sub1.TopMenuID)) {
<li>@Html.ActionLink(sub2.Descricao,sub2.Action,sub2.Controller)</li>
}
</ul>
</li>
}else{
<li>@Html.ActionLink(sub1.Descricao,sub1.Action,sub1.Controller)</li>
}
}
</ul>
</li>
}else{
<li>@Html.ActionLink(item.Descricao,item.Action,item.Controller)</li>
}
}
}
</ul>
</div>
</div>
</div>
This is the TopMenu class
public class TopMenuItem {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int Parent { get; set; }
public bool Group { get; set; }
public string Descricao { get; set; }
public string Action { get; set; }
public string Controller { get; set; }
}
All this will lead to empty lists, for example a clean database. But, when I call up a list of actions, for example. The conflict arises because the view of the stand ("List.cshtml" and "TopBar.cshtml") begins with:
@model IEnumerable<SIGO.Models.Employee>
or
@model IEnumerable<SIGO.Models.TopMenu>
PS: I do not use a controller to process data in TopMenu.
Questions
!