I am using MVC Sitemap provider for my site, and I show up to three versions of the navigation on my page at a time.
First, the top level:
@Html.MvcSiteMap("MvcSiteMapProvider").Menu(0, true, false, 1)
Sub sections, which is reused when viewing a top-level page:
@Html.MvcSiteMap("MvcSiteMapProvider").Menu(2, 1, true)
Here is a 2 level section in my sitemap:
<mvcSiteMapNode title="Bid Manager" description="This is an example of a section description" controller="Bid" action="Index" icon="(">
<mvcSiteMapNode title="Create / Edit Sale Event" description="This is an example of a section description" controller="Bid" action="Manage" icon=")" />
<mvcSiteMapNode title="Bidding Status Manager" description="This is an example of a section description" controller="Bid" action="Status" icon="]" />
</mvcSiteMapNode>
...and here is the display template
@model MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
@using System.Web.Mvc.Html @using MvcSiteMapProvider.Web.Html.Models
@{ // Defaults string itemIcon = "1"; string listClass = "menu-item";
// Pick up options in custom parameters
if (Model.MetaAttributes.ContainsKey("icon")) {
itemIcon = Model.MetaAttributes["icon"].ToString();
}
if (Model.MetaAttributes.ContainsKey("disabled") && (Model.MetaAttributes["disabled"].ToString() == "true")) {
listClass = "disabled";
itemIcon = "U";
}
// Check if this node is active
listClass = (Model.IsCurrentNode || Model.IsInCurrentPath) ? "active" : listClass;
// Show stuff
<li class="@listClass">
<div class="menu-icon" aria-hidden="true" data-icon="@Html.Raw(itemIcon)"></div>
<p>
@if (listClass == "disabled") {
<span>@Model.Title</span>
}
else {
<a href="@Model.Url">@Model.Title</a>
}
</p>
<p class="description">@Model.Description</p>
</li>
}
I have it so that the display template is identical for each instance of the sub-level navigation with all cosmetic differences via CSS. What I want to do is conditionally include the description attribute, e.g. only render the description.
Currently, the description is rendered for all menus and then hidden with CSS for those where I don't want it, which is obviously a dirty fix.
This may well be a more broad MVC questions, such as setting a variable before calling the sitemap, then using that variable in the display template.
Any suggestions would be appreciated.
Thanks!