I have a node with a listing of other children and each one has the property "IsAdditionalProgram". I am running this to check for the true/false value and list as 2 separate navigation items according to that value. Some of the nodes are randomly being added to the wrong list though. What is wrong with my code?
@{
var root = Model.AncestorOrSelf(4);
var program = root.Children.Where("Visible");
<div id="left-nav">
<ul>
@foreach(var item in program){
if(item.HasValue("isAdditionalProgram") != true){
<li @Model.IsDescendantOrSelf(item,"class=selected", "")><a href="@item.Url">@item.Name</a></li>
}
}
</ul>
</div>
}
This is the second navigation list based off the same children
@{
var list = Model.AncestorOrSelf(4).Children.Where("Visible");
<div id="nav-additional-wrap">
<div id="left-nav" class="left-nav-additional">
<ul>
@foreach(var item in list){
if(item.HasValue("isAdditionalProgram") == true){
<li @Model.IsDescendantOrSelf(item,"class=selected", "")><a href="@item.Url">@item.Name</a></li>
}
}
</ul>
</div>
</div>
}
The HasValue function will only tell you whether the isAdditionalProgram property has been set, not what value it is.
So if one node has its value set to false the HasValue method will return still true, hence why it might appear in the wrong list.
What you want to do is the following:
if (!item.HasValue("isAdditionalProgam") || item.GetValue("isAdditionalProgram") == false)
(i.e. only add when isAdditionalProgram has not been set, or is set to false)
And for the other list:
if (item.HasValue("isAdditionalProgram") && item.GetValue("isAdditionalProgram") == true)
(i.e. only add when isAdditionalProgram has been set and is set to true)
HasValue Producing Some Irregular Output
I have a node with a listing of other children and each one has the property "IsAdditionalProgram". I am running this to check for the true/false value and list as 2 separate navigation items according to that value. Some of the nodes are randomly being added to the wrong list though. What is wrong with my code?
This is the second navigation list based off the same children
The
HasValue
function will only tell you whether theisAdditionalProgram
property has been set, not what value it is. So if one node has its value set tofalse
theHasValue
method will return stilltrue
, hence why it might appear in the wrong list.What you want to do is the following:
(i.e. only add when
isAdditionalProgram
has not been set, or is set tofalse
)And for the other list:
(i.e. only add when
isAdditionalProgram
has been set and is set totrue
)Tom,
When using your code I get a listing of all the children in the first nav and nothing in the second nav list.
Not sure why that would be, but try this version:
Hi Steve,
Here a some razor documentation about the true/false property editor, in razor. The documentation are the same for version 7, 6 and 4.
http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/Property-Editors/Built-in-Property-Editors-v7/True-False
or http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/Property-Editors/Built-in-Property-Editors/True-False But as you can see the examples for the typed and dynamic razor are the same.
Hope this can help you in the right direction,
/Dennis
I finally got it working correctly with this: (Thanks for your efforts!!)
And this is the second list
is working on a reply...