How to prevent a node from appearing in breadcrumbs MVC

705 Views Asked by At

There are certain nodes in my solution that need to appear in menus/navigation but not appear in the breadcrumbs, as they do not link to actual pages and therefore just present an error when clicked. I've tried several of the techniques pertaining to advanced node visibility from here: https://github.com/maartenba/MvcSiteMapProvider/wiki but I can't seem to get it to work for me. A coworker suggested possibly designing an overload to the SiteMapPath method in the SiteMapPathHelper class, but I'm not sure if this is accurate or how it would be formatted. I'm fairly new to MVC and could really use some help. Thanks!


Thank you @NightOwl888 for your response! I'm going to add a few more details to make sure I'm getting things correct. This is an example of what the section of nodes in the Sitemap file looks like:

<mvcSiteMapNode title="Inventory Management" url="~/Resources/Inventory/Default.aspx"> <mvcSiteMapNode title="Group Inventory" action="#" visibility="SiteMapPathHelper,!*" clickable="false"> <mvcSiteMapNode title="Group Inventory Management" url="~/Resources/Inventory/UnitInventory" controller="GroupInventory" action="Index" /> <mvcSiteMapNode title="Manage All Group Inventory" url="~/Resources/Inventory/ManageAllGroupInventory.aspx"> <mvcSiteMapNode title="Manage Inventory Item" url="~/Resources/Inventory/ManageInventoryItem.aspx" /> </mvcSiteMapNode> <mvcSiteMapNode title="Search Inventory" url="~/Resources/Inventory/SearchInventory.aspx?module=resource" /> </mvcSiteMapNode> </mvcSiteMapNode>

"Inventory Management" represents the initial page the user navigates to, where a menu is displayed with the heading "Group Inventory" (the second node), and the menu items are the items below that ("Group Inventory Management", "Manage All Group Inventory", and "Search Inventory"). Each of the menu items are links to other pages, but the menu header is not a link and is not clickable. When I navigate to one of the menu items, the menu header ("Group Inventory") shows up in the breadcrumbs display, even though it doesn't link to anything. So basically if I click on the first menu item, it displays "Home > Inventory Management > Group Inventory > Group Inventory Management", and if the user clicks on "Group Inventory" it results in an error. I need to prevent "Group Inventory" from displaying in just the breadcrumbs trail. I'm struggling a bit to figure this out from your initial response.

P.S. Some of the attributes in the Group Inventory node are ones I added to try to hide it, based on examples I've seen so far.

2

There are 2 best solutions below

2
On

A SiteMapNode is a representation of a real location on your web site or an external site (except for a non-clickable grouping node - that is a node with the Clickable property set to false, which is just for ensuring you still have a hierarchy for a node that doesn't represent a real location). SiteMapNodes are a server-side representation of nodes - you can think of it like a hierarchial database.

If you have additional links you would like added to the menu on the client side (perhaps for some JavaScript functionality), you can customize the menu helper templates in the /Views/Shared/DisplayTemplates/ directory. If you need to, you can add your own custom attributes to specific nodes to signal your templates exactly where to add your client-side links.

See MvcSiteMapProvider hidden mvcSiteMapNode with CRUD operation for more of an idea how to accomplish this.

Related:


On your non-clickable grouping node, you have specified for visibility: visibility="SiteMapPathHelper,!*".

Assuming you have setup the default visibility provider as FilteredSiteMapNodeVisibilityProvider as per the documentation, you are specifically telling it "make the node visible on the breadcrumb trail, but not anywhere else" (menu, site map, XML site map, etc). So, it is doing exactly what you have specified to do. However, you haven't specified what you are expecting it to do. Perhaps you meant !SiteMapPathHelper, which means "make the node visible everywhere but the breadcrumb trail"?

0
On

I was able to find a solution somewhat outside the realm of what I expected. I added an attribute to the specific nodes I needed to hide named "isbreadcrumbhidden". Then I was able to put a check in SiteMapPathHelperModel.cshtml, the view that builds the breadcrumb string, to exclude any nodes that have the "isbreadcrumbhidden" attribute set to "true". I like the solution because I don't have to make any extensive changes or additions to the code, just the single attribute. Below is the code from the view:

@foreach (var node in Model) {
    if (!node.Attributes.ContainsKey("isbreadcrumbhidden") || (node.Attributes.ContainsKey("isbreadcrumbhidden") && node.Attributes["isbreadcrumbhidden"].ToString() == "false"))
    {
        @Html.DisplayFor(m => node);

            if (node != Model.Last()) {
                <text> &gt; </text>
        }
    }
}