Search code examples
asp.net-mvcrazorbreadcrumbsmvcsitemapprovider

MvcSiteMapProvider on View side with razor get parents of current node


I am using MVC Site Map Provider for creating some breadcrumbs

what i wanna do; creating breadcrumbs on view using razor

i would like to do some calls like shown below

<div id="navbar">
    <div>YOU ARE HERE:
        // There is no calls like this but you get the idea
        @if (SiteMap.CurrentNode.HasParentNodes == true) 
        {
            // I'd like to loop trough each parent till the RootNode
            foreach (SiteMapNode Snode in SiteMap.CurrentNode.ParentNodes)
            {
                <a href="@Snode.Url">@Snode >></a> 
            }
        } else {
            // If there is no parent just show current node
            <a href="@SiteMap.CurrentNode.Url.ToString()">@SiteMap.CurrentNode</a>
        }
    </div>
    <div>@DateTime.Now.Date.ToLongDateString()</div>
</div>

so how can i achieve this using Razor on View?


Solution

  • The @Html.MvcSiteMap().SiteMapPath() HTML helper already does this. It is a templated control that you can modify by editing the /Views/Shared/DisplayTemplates/SiteMapPathHelperModel.cshtml file, so basically you can get whatever HTML output you want.

    If you still prefer to do it your way after knowing that, you can also do this with MvcSiteMapProvider.SiteMaps.Current.CurrentNode.Ancestors property (version 4 and up).