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
    Apr 21, 2014 @ 20:55
    Steve
    0

    If Statement with Multiple Conditions?

    I have a razor that is building a dynamic navigation and I need it to only build the nested <ul> if two conditions are met. The script worked fine without the test for Children.Where("Visible"). When I was using Children.Any() it worked,but it would create an empty <ul> if there was a child node hidden from navigation. Here is my razor:

    @{
    var page = Model.AncestorOrSelf(3).Children.Where("Visible");
    
    <ul>
    @foreach (var subPage in page) {
        @*  var noLink = subPage.HasValue("noLinkNav") ? "class=noLink" : ""; if you want to style the external link to not display in the navigation for noLinkNav property *@
          var style = subPage.IsDescendantOrSelf(Model) ? "class=active" : "";
          var targetParent = subPage.externalUrl ? "target=_blank" : "";
          var linkParent = subPage.HasValue("externalUrl") ? subPage.url : subPage.Url;
        <li @style><a href="@linkParent" @targetParent>@subPage.Name</a>
    
    @if(subPage.Children.Any() && subPage.IsAncestorOrSelf(Model)){ 
        <ul class="academics-items">
            @foreach (var item in subPage.Children.Where("Visible")) {
                var childActive = item.IsEqual(Model) ? "class=activeChild" : "";
                var target = item.externalUrl ? "target=_blank" : "";
                var link = item.HasValue("externalUrl") ? item.url : item.Url;
                <li @childActive ><a href="@link" @target>@item.Name</a>
    
    @if(item.Children.Any() && item.IsAncestorOrSelf(Model)){ 
        <ul class="academics-items">
            @foreach (var child in item.Children.Where("Visible")) {
                var subChildActive = child.IsEqual(Model) ? "class=subActive" : "";
                var subTarget = child.externalUrl ? "target=_blank" : "";
                var subLink = child.HasValue("externalUrl") ? child.url : child.Url;
                <li @subChildActive ><a href="@subLink" @subTarget>@child.Name</a>
    
    @if(child.Children.Any() && child.IsAncestorOrSelf(Model)){ 
        <ul class="academics-items">
            @foreach (var lastChild in child.Children.Where("Visible")) {
                var lastChildActive = lastChild.IsEqual(Model) ? "class=lastActive" : "";
                var lastChildTarget = lastChild.externalUrl ? "target=_blank" : "";
                var lastChildLink = lastChild.HasValue("externalUrl") ? lastChild.url : lastChild.Url;
                <li @lastChildActive ><a href="@lastChildLink" @lastChildTarget>@lastChild.Name</a></li>
            }
        </ul>
                }
                </li>
            }
                        </ul>
                    }
                        </li>
            }
        </ul>
       } 
        </li>
    }
    </ul>
    
    }
  • Steve 472 posts 1216 karma points
    Apr 21, 2014 @ 21:28
    Steve
    0

    Okay, I've gotten it working correctly, but could someone please tell me why I had to add the ".Count() > 0" in my if statement to get it working?

    @if(subPage.Children.Where("Visible").Count() > 0 && subPage.IsAncestorOrSelf(Model)){ 

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies