Reputation: 11
I am trying to display subnav links separately within the layout (theme).
I've overridden the MenuItem.cshtml that only displays the 'root' level nav links. But now I'm trying to figure out how to get access to the Menu.Items from the Model in the Layout.cshtml.
What would I cast the Model to that would expose the Menu.Items?
example: products 1.0, support 2.0
when viewing the products page you would see electronics 1.1 furniture 1.2 office supplies 1.3
Upvotes: 0
Views: 773
Reputation: 11
I didn't have much luck with Advanced Menu. There were some bugs in it that prevented me to use it.
My solution I found isn't the prettiest code and needs to be refactored but here it is: I render a PartialView casting the Model.Navigation
code for the Layout.cshtml where I want the second (sub menu to display)
@{Html.RenderPartial("SubNavMenu", (IEnumerable)Model.Navigation);}
code in the PartialView
@model IEnumerable<dynamic>
@{
IEnumerable<dynamic> subNavLinks = null;
string requestUrl = Request.Path.Replace(Request.ApplicationPath, string.Empty).TrimEnd('/').ToUpperInvariant();
IEnumerable<dynamic> children = Model.FirstOrDefault(x => x.MenuName == "main").Items;
var menuItems = children.Select(x => x.Item);
var parent = menuItems.FirstOrDefault(x => x.Text.TextHint.Equals(requestUrl, StringComparison.InvariantCultureIgnoreCase));
if(parent!=null)
{
subNavLinks = menuItems.Where(x => x.Position.StartsWith(parent.Position.Substring(0,1)) && x.Position.Length>1);
}
}
<nav>
<ul class="subNav">
@if (subNavLinks != null)
{
foreach (var item in subNavLinks)
{
<li><a class="@className" href="@item.Href">@item.Text</a></li>
}
}
</ul>
</nav>
Upvotes: 1
Reputation: 4744
I do not know what you want to achieve exactly, but I recommand you take a look at the Advanced Menu module: it features easy to use hierarchical menus and menu widget you can include in your layout to display independent menus or submenus easily.
Upvotes: 0