Question

I would like to be able to show the title of the parent of the current node.

Sitemap:

    <mvcSiteMapNode title="Main Site Tile" controller="Home" action="Index">
  <mvcSiteMapNode title="Some site section" controller="Section" action="Index">
    <mvcSiteMapNode title="Some page" controller="Section" action="Page1" />
    <mvcSiteMapNode title="Some other page" controller="Section" action="Page2" />
  </mvcSiteMapNode>
</mvcSiteMapNode>

So, on 'Page1' I can show the title (Some page) by using:

Html.MvcSiteMap().SiteMapTitle()

Great. But, I would also like to show the title of the parent node (Some site section) in my _Layout page, so this would appear the same if I were viewing 'Page1' or 'Page2' (or indeed any view nested within).

Is this possible?

Was it helpful?

Solution

You can do this using the SiteMapTitle attribute and set the Target to ParentNode.

[MvcSiteMapProvider.Web.Mvc.Filters.SiteMapTitle("SomeKey", Target = AttributeTarget.ParentNode]
public ViewResult Show(int blogId) { 
   ViewData["SomeKey"] = "This will be the title";

   var blog = _repository.Find(blogId); 
   return View(blog); 
}

That will set the title of the links in the Menu and SiteMapPath.

You can also access the nodes programmatically using the static SiteMaps object.

var theTitle = MvcSiteMapProvider.SiteMaps.Current.CurrentNode.ParentNode.Title;

Alternatively, you can use the FindSiteMapNodeFromKey("theKey") method to find any node in the SiteMap to get its title.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top