ASP.NET MVC 4 Breadcrumbs with dynamic list and parameters

12,554

I'd suggest you use the MVC Sitemap provider and the bread crumb trails from it. You will need to build a Dynamic Node Provider, but that's fairly straightforward - just follow the steps to use Dynamic Nodes

One of the tricks when building dynamic nodes is to make sure that you foreach across each node at each level, but add it to the nodes collection at the top.

        var nodes = new List<DynamicNode>();
        foreach (var eventCategory in eventCategories)
        {
            var node = new DynamicNode
                {
                    Key = string.Format("{0}_{1}", "home_events", eventCategory.Name),
                    ParentKey = "home_events",
                    Title = pluralize.Pluralize(eventCategory.Name),
                    Description = eventCategory.Description,
                    Controller = "event",
                    Action = "category"
                };
            node.RouteValues.Add("slug", eventCategory.Slug);
            nodes.Add(node);

            foreach (var e in eventCategory.Events)
            {
                var eventNode = new DynamicNode
                {
                    Key = string.Format("{0}_{1}", eventCategory.Name, e.Name),
                    ParentKey = node.Key,
                    Title = e.Name,
                    Controller = "event",
                    Action = "event"
                };
                eventNode.RouteValues.Add("categorySlug", eventCategory.Slug);
                eventNode.RouteValues.Add("slug", e.Slug);
                nodes.Add(eventNode);
            }
        }

and then it's just a case of putting the breadcrumbs in like this

Html.MvcSiteMap().SiteMapPath()

home > events > marathons > melbourne marathon

Share:
12,554
Siegmund Nagel
Author by

Siegmund Nagel

Updated on July 20, 2022

Comments

  • Siegmund Nagel
    Siegmund Nagel almost 2 years

    Basically what I'm trying to do is create a list of breadcrumbs. That list of dynamic (as you drill down to deeper pages the list of breadcrumbs grows) and most links have parameters. I've seen examples with MVCSiteInfo but a little confused as most of the examples I've seen focus on a static set of links with no parameters. I can't imagine this isn't a common problem so I was wondering what's the best way to accomplish this functionality.

    Thanks in advance!

    Sieg