All my recent projects has implemented umbracoNaviHide inside a True/False property, and they all work just fine. In my 7.6.9 upgraded project, I simply cannot get a True/False value to work.
On my Master composition, I have a "Navigation" tab together with a True/False property. All my pages inherit through that composition.
When I do something like:
var menuItems = CurrentPage.Site().Children;
@foreach(var item in menuItems) {
if(item.IsVisible()) {
<!--show element-->
}
}
It doesn't work. It should work, as it has in all my lastest projects. Here's what I've tried:
var menuItems = CurrentPage.Site().Children.Where("Visible");
var menuItems = CurrentPage.Site().Children.Where("umbracoNaviHide == false"); //setting this to true gives me no results
var menuItems = CurrentPage.Site().Children.Where(x => x.IsVisible());
var menuItems = CurrentPage.Site().Children.Where("randomProperty == false"); //or true
foreach(var item in menuItems) {
if(!item.HasValue("umbracoNaviHide)) { <!-- show item --> }
if(item.GetPropertyValue("umbracoNaviHide")) == "false") { <!-- show item --> }
}
I think that's all. I did update my ModelsBuilder. I did restart the project. I did remove the property and readd it. I basically tried everything, but it just won't work.
What on earth?
UPDATE: Check out this. This is soooooooooooooooooooooooooooooooooooo weird:
@foreach(var item in menuItems) {
<p>@item.GetPropertyValue("umbracoNaviHide") - @item.Name</p>
}
This gives me the following:
FALSE - Our offers
FALSE - Technology
TRUE - Projects
FALSE - About us
FALSE - Gallery
TRUE - Contact
However, this is how the umbracoNaviHide
is setup in the backoffice:
Ticked - Our offers
Not ticked - Technology
Ticked - Projects
Not ticked - About us
Not ticked - Gallery
Ticked - Contact
How the flying hell is this possible?
Ok, everything's clear now :)
You're using dynamic objects and you're trying to treat them as IPublishedContent. If they are not mapped correctly it behave weird and unpredictable.
As dynamic objects will be removed in the following Umbraco versions, I suggest to use strongly typed IPublishedContent objects or take advantage of ModelsBuilder and operate on generated models of specific types.
My test code is below. Both versions works in my test 7.6.9 solution.
// Dynamic object version - will be deprecated - don't use!
//var visibleChildItems = CurrentPage.Site().Children.Where("Visible");
// Strongly Typed version - suggested
var visibleChildItems = Model.Content.Site().Children(x => x.IsVisible());
<ul>
@foreach (var item in visibleChildItems)
{
<li>
<a href="@item.Url">@item.Name</a>
</li>
}
</ul>
Hope it will solve your problem. If you'll be interested about operating through dynamic objects and how to get properties from them, check a little bit old, but still valid Razor Cheetsheet for Umbraco: https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets.