MVCSiteMapProvider - 显示父节点的标题

Ala*_*tis 2 asp.net-mvc asp.net-mvc-4 mvcsitemapprovider

我希望能够显示当前节点的父标题.

网站地图:

    <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>
Run Code Online (Sandbox Code Playgroud)

因此,在'Page1'上我可以使用以下方式显示标题(某些页面):

Html.MvcSiteMap().SiteMapTitle()
Run Code Online (Sandbox Code Playgroud)

大.但是,我还想在我的_Layout页面中显示父节点(某些网站部分)的标题,所以如果我查看'Page1'或'Page2'(或者实际上嵌套在其中的任何视图),这将显得相同.

这可能吗?

Nig*_*888 5

您可以使用SiteMapTitle属性执行此操作,并将Target设置为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); 
}
Run Code Online (Sandbox Code Playgroud)

这将在Menu和SiteMapPath中设置链接的标题.

您还可以使用静态SiteMaps对象以编程方式访问节点.

var theTitle = MvcSiteMapProvider.SiteMaps.Current.CurrentNode.ParentNode.Title;
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用该FindSiteMapNodeFromKey("theKey")方法在SiteMap中查找任何节点以获取其标题.