Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Steve 472 posts 1216 karma points
    Sep 15, 2014 @ 21:34
    Steve
    0

    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?

    @{
    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>
    }
  • Tom van Enckevort 107 posts 429 karma points
    Sep 15, 2014 @ 21:45
    Tom van Enckevort
    0

    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)

  • Steve 472 posts 1216 karma points
    Sep 15, 2014 @ 21:57
    Steve
    0

    Tom,

    When using your code I get a listing of all the children in the first nav and nothing in the second nav list.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Sep 15, 2014 @ 22:31
    Nicholas Westby
    1

    Not sure why that would be, but try this version:

    item.GetPropertyValue<bool>("isAdditionalProgram")
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Sep 15, 2014 @ 22:43
    Dennis Aaen
    0

    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.

    @{ 
        foreach (IPublishedContent page in Model.Content.Children){
            if (!page.GetPropertyValue<bool>("umbracoNaviHide")) {
                @page.Name
            }
        }    
    }

    Hope this can help you in the right direction,

    /Dennis

  • Steve 472 posts 1216 karma points
    Sep 15, 2014 @ 22:51
    Steve
    0

    I finally got it working correctly with this: (Thanks for your efforts!!)

    @{
    var root = Model.AncestorOrSelf(4);
    var program = root.Children.Where("Visible");
    
    <div id="left-nav">
        <ul>
        @foreach(var item in program){
            if (item.Where("isAdditionalProgram != true")){
            <li @Model.IsDescendantOrSelf(item,"class=selected", "")><a href="@item.Url">@item.Name</a></li>
        }
        }
        </ul>
    </div>
    }

    And this is the second list

    @{
        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.Where("isAdditionalProgram  == true")){
                <li @Model.IsDescendantOrSelf(item,"class=selected", "")><a href="@item.Url">@item.Name</a></li>
                }
            }
        </ul>
            </div>
            </div>
    }
Please Sign in or register to post replies

Write your reply to:

Draft