Reputation: 16012
I wish to override the title for the sitemap node, but only in the breadcrumb, not in the menu.
[Route("{partyAccountNumber}")]
[SiteMapTitle("DisplayName", Target = AttributeTarget.CurrentNode)]
public ActionResult Advice(string partyAccountNumber)
How do I use this overridden title, but only in the breadcrumb, and not in the menu.
Upvotes: 0
Views: 1868
Reputation: 56869
You can put conditional logic on the title by editing the /Views/Shared/DisplayTemplates/SiteMapNodeModel.cshtml
file directly.
@model MvcSiteMapProvider.Web.Html.Models.SiteMapNodeModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models
@* Conditionally set the title based on a custom attribute *@
@{ var title = Model.Title; }
@if (Model.SourceMetadata["HtmlHelper"].ToString() == "MvcSiteMapProvider.Web.Html.SiteMapPathHelper" &&
Model.Attributes.ContainsKey("breadcrumbTitle") &&
!string.IsNullOrEmpty(Model.Attributes["breadcrumbTitle"].ToString()))
{
title = Model.Attributes["breadcrumbTitle"].ToString();
}
@if (Model.IsCurrentNode && Model.SourceMetadata["HtmlHelper"].ToString() != "MvcSiteMapProvider.Web.Html.MenuHelper") {
<text>@title</text>
} else if (Model.IsClickable) {
if (string.IsNullOrEmpty(Model.Description))
{
<a href="@Model.Url">@title</a>
}
else
{
<a href="@Model.Url" title="@Model.Description">@title</a>
}
} else {
<text>@title</text>
}
On your node, you could set a custom attribute to provide the alternate value for the breadcrumb trail. If desired, you can also load the titles from a dynamic source, such as a database by using a dynamic node provider.
public class MyDynamicNodeProvider : DynamicNodeProviderBase
{
public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
{
// Entities would be your entity framework context class
// or repository.
using (var entities = new Entities())
{
// Create a node for each item (data row in the table)
foreach (var item in entities.Items)
{
DynamicNode dynamicNode = new DynamicNode();
dynamicNode.Title = item.Title;
// Provide a custom attribute for the breadcrumb title.
dynamicNode.Attributes.Add("breadcrumbTitle", item.BreadcrumbTitle);
dynamicNode.ParentKey = "Home";
dynamicNode.Key = "Item_" + item.Id;
dynamicNode.Controller = "Item";
dynamicNode.Action = "Advice";
// custom route values must be manually accounted for by using
// RouteValues or PreservedRouteParameters.
dynamicNode.RouteValues.Add("partyAccountNumber", item.PartyAccountNumber);
//dynamicNode.PreservedRouteParameters.Add("partyAccountNumber");
yield return dynamicNode;
}
}
}
}
And then wire your dynamic node provider into your node configuration.
// Set a key explicitly to attach the dynamic nodes to.
// The key property here corresponds to the ParentKey property of the dynamic node.
<mvcSiteMapNode title="Home" controller="Home" action="Index" key="Home">
// Use a "dummy" node for the dynamic node provider. This node won't be in the SiteMap.
<mvcSiteMapNode dynamicNodeProvider="NamespaceName.MyDynamicNodeProivder, AssemblyName"/>
</mvcSiteMapNode>
Upvotes: 1