Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 Breadcrumbs with dynamic list and parameters

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

like image 227
Siegmund Nagel Avatar asked Feb 27 '13 15:02

Siegmund Nagel


1 Answers

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

like image 107
Pete - MSFT Avatar answered Sep 22 '22 11:09

Pete - MSFT