In a project, i've implemented MvcSiteMapProvider that work great. This is a side menu generated with @Html.MvcSiteMap().Menu()
Here is a node of the menu (Mvc.sitemap file).
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="About" controller="Home" action="About"/>
<mvcSiteMapNode title="Project" controller="Home" action="DummyAction">
<mvcSiteMapNode title="List" controller="Home" action="Project"/>
<mvcSiteMapNode title="Edit" controller="Home" action="Edit" preservedRouteParameters="id" visibility="SiteMapPathHelper,!*"/>
</mvcSiteMapNode>
When I load the "List" from "Project", it displays a page with all projects and I can select one of them to load the Edit action with the related ID of the project.
The problem is, when I'm in the Edit action page, the side menu is all collapsed, but I'm expecting to have the "Project" node opened. If I add the node "Edit" in the menu, it work (matching node action), but I don’t want this node because its useless for the user.
Also, I've tried the DefaultSiteMapNodeVisibiltyProvider, I can hide the "Edit" node if added, but when I'm in the "Edit" action page, the Project node is also closed.
I'll face the same problem for the "New/Add" operation that I don’t want to see in the side menu, but will be accessible from a link in the project list. However, for these operations, I want to let the user know that it is inside the "Project" section, with the "Project" node opened.
See this project on GitHub: Project on Github
Best regards,
Your issue appears to be that you haven't accounted for the recursive nature of the
Menu
HTML helper. There are 3 templates that theMenu
interacts with:MenuHelperModel.cshtml
SiteMapNodeModelList.cshtml
SiteMapNodeModel.cshtml
The problem is that you haven't added the appropriate logic to the
SiteMapNodeModelList.cshtml
. In addition, the logic in theMenuHelperModel.cshtml
is overly complex.A good approach for dealing with both a
Menu
andSiteMapPath
HTML helper on the same page with customizations is to use named templates rather than editing the default templates.BootstrapMenuHelperModel.cshtml
BootstrapMenuNodeModelList.cshtml
Usage
Here, we tell the
Menu
HTML helper to use our custom template namedBootstrapMenuHelperModel.cshtml
.