Reputation: 575
I need to use MVCSitemap provider in a web application, but I can't make it work where I have dynamic urls.
I have a list of categories, which can have parents and children. For example if I click on a category, the breadcrumb looks like this:
Home > Filter
if I click on a children of the Filter, I got:
Filter > Air filter
the Home link disappears. If I click on the childrens of the "Air filter" I got:
Air filter > air filter children
and so on. Always the two last levels are showed, and if I click on the first level, always goes back to the Home page.
This is in my MvcSitemap:
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="Product" controller="Product" action="SubCategories" preservedRouteParameters="selected,category,id,engineId">
<mvcSiteMapNode title="Details" controller="Product" action="ProductDetails" preservedRouteParameters="supplierName,code,name,prodId,-1"/>
</mvcSiteMapNode>
</mvcSiteMapNode>
This is the Subcategories method from Product controller:
[MvcSiteMapNode(Title = "Article", ParentKey = "SubCategories")]
[Route("{selected}-{category}-{id}-{engineId}")]
public ActionResult SubCategories(string selected, string category, int id, string engineId)
{
...........................
SiteMaps.Current.CurrentNode.Title = categoryName;
if(categoryRepository.GetCategoryByID(id).ParentId.HasValue)
{
int parentId = categoryRepository.GetCategoryByID(id).ParentId.Value;
string parentName = categoryRepository.GetCategoryByID(parentId).Name;
SiteMaps.Current.CurrentNode.ParentNode.RouteValues["id"] = id;
SiteMaps.Current.CurrentNode.ParentNode.Title = parentName;
}
Can you please help me, what I'm doing wrong here? I checked all the explanations on the web, I tried in many ways, but none of them resolved this problem.
Upvotes: 4
Views: 911
Reputation: 2245
Here is my sample. You can refer it.
The first, I have a sitemap:
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="About" controller="Home" action="About" />
<mvcSiteMapNode title="Contact" controller="Home" action="Contact" />
<mvcSiteMapNode title="Administration" clickable="false">
<mvcSiteMapNode title="User Mgmt" controller="Administration" action="UserMgmt" clickable="false" >
<mvcSiteMapNode title="List Role" controller="Administration" action="ListRole" >
<mvcSiteMapNode title="Details" controller="Administration" action="Details">
<mvcSiteMapNode title="XXX" controller="Administration" action="XXX" />
</mvcSiteMapNode>
</mvcSiteMapNode>
</mvcSiteMapNode>
<mvcSiteMapNode title="Role Mgmt" controller="Home" action="RoleMgmt" />
</mvcSiteMapNode>
</mvcSiteMapNode>
//And then, I create a BootstrapMenuHelperModel view that load this sitemap. //I putted it at DisplayTemplates.
@model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
@using MvcSiteMapProvider.Web.Html.Models
@helper TopMenu(List<SiteMapNodeModel> nodeList)
{
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
@foreach (SiteMapNodeModel node in nodeList)
{
string url = node.IsClickable ? node.Url : "#";
if (!node.Children.Any())
{
<li><a href="@url">@node.Title</a></li>
}
else
{
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown">@node.Title <span class="caret"></span></a>
@DropDownMenu(node.Children)
</li>
}
if (node != nodeList.Last())
{
<li class="divider-vertical"></li>
}
}
</ul>
</div>
</div>
</nav>
}
@helper DropDownMenu(SiteMapNodeModelList nodeList)
{
<ul class="dropdown-menu" role="menu">
@foreach (SiteMapNodeModel node in nodeList)
{
if (node.Title == "Separator")
{
<li class="divider"></li>
continue;
}
string url = node.IsClickable ? node.Url : "#";
if (!node.Children.Any())
{
<li><a href="@url">@node.Title</a></li>
}
else
{
<li class="dropdown-submenu"><a href="@url">@node.Title</a>@DropDownMenu(node.Children)</li>
}
}
</ul>
}
@TopMenu(Model.Nodes)
//Finally, call this view from layout
<div class="row">
<div class="span12">
<nav>
@Html.MvcSiteMap().Menu("BootstrapMenuHelperModel")
</nav>
</div>
</div>
Upvotes: 1