GraemeMiller
GraemeMiller

Reputation: 12253

How do I specify an MVC SiteMaps Visibility attribute using Code based attribute?

I am using the latest Nuget package of MVC SiteMap provider. We are making heavy use of code based attributes defining nodes in our site.

E.g. [MvcSiteMapNode(Title = "Examination Types", ParentKey = "LookupTable", Key = "ExaminationTypeIndex")]

We want to make use of a custom visibility provider to hide nodes from SiteMap as per here

However we can't seem to specify node visibility attribute using Code based nodes? Is there anyway to do that. We can only specify a custom visibility provider and we would rather use the visible attribute.

Upvotes: 1

Views: 1669

Answers (2)

NightOwl888
NightOwl888

Reputation: 56859

Visibility is a custom attribute, so you need to supply it in the Attributes field in order to use it in conjunction with [MvcSiteMapNodeAttribute]. The only tricky part is that .NET attributes don't support dictionary types, so you need to supply the attributes as an escaped JSON string.

[MvcSiteMapNode(Title = "Examination Types", ParentKey = "LookupTable", Key = "ExaminationTypeIndex", Attributes = @"{ ""visibility"": ""SiteMapPathHelper,!*"" }")]

NOTE: If you need to supply multiple custom attributes, separate them with a comma.

Attributes = @"{ ""visibility"": ""SiteMapPathHelper,!*"", ""myCustomAttribute"": true }"

Upvotes: 2

Wayne Brantley
Wayne Brantley

Reputation: 694

The only way I found to do this is to create several custom visibility providers. For example, if your visibility flag could be 'true' and 'false'....then you would simply make a visiblity provider that always returned false. In the sitemapnodes you create in code - specify that provider.

If you do not want to do that on every attribute, make a new class that inherits from MvcSiteMapNodeAttribute that sets the proper visibility provider - then use that where appropriate.

Upvotes: 1

Related Questions