سؤال

I'm trying to build a breadcrumb trail that will be represented by a list of site map nodes. the root node will be first, then the child, grandchild....... up until the current node. I'm trying to do this with recursion, but always getting the root node only:

public static List<MvcSiteMapNode> BreadcrumbTrail(MvcSiteMapNode curr)
    {
        List<MvcSiteMapNode> t = new List<MvcSiteMapNode>();
        if (curr.ParentNode == null)
        {
            t.Add(curr);
            return t;
        }
        else
            return BreadcrumbTrail(curr.ParentNode as MvcSiteMapNode);
    }

and the caller:

var curr = SiteMap.CurrentNode as MvcSiteMapNode;

        List<MvcSiteMapNode> trail = BreadcrumbTrail(curr);
هل كانت مفيدة؟

المحلول

You only get the root node because you're only adding the root node. You can fix this by also adding the current node to the result from the recursion.

public static List<MvcSiteMapNode> BreadcrumbTrail(MvcSiteMapNode curr)
    {
        List<MvcSiteMapNode> t;
        if (curr.ParentNode == null)
            t = new List<MvcSiteMapNode>();
        else
            t = BreadcrumbTrail(curr.ParentNode as MvcSiteMapNode);
        t.Add(curr);
        return t;
    }

should do what you're looking for

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top