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
    May 12, 2014 @ 16:02
    Steve
    0

    Help with Navigation

    Hello,

    I am generating a dynamic nav on my page using a traverse helper, but I need to find a way to prevent it from creating an empty <ul> tag for the navigation when there are no children of the current page to display. Here is what I have.

    @{
    var StartLevel = String.IsNullOrEmpty(Parameter.startLevel) ? 3 : int.Parse(Parameter.startLevel);
    var FinishLevel = String.IsNullOrEmpty(Parameter.finishLevel) ? 7 : int.Parse(Parameter.finishLevel);
    var page = @Model.AncestorOrSelf(StartLevel);
        if(page != null)
        {
        @traverse(page, StartLevel, FinishLevel);   
        }                
    }   
    
    @helper traverse(dynamic page, int StartLevel, int FinishLevel)
    {
        <ul class="nodeList">
            @foreach (var node in page.Children.Where("Visible && NodeTypeAlias == \"Category2InternalPage\" || NodeTypeAlias == \"ExternalLink\" || NodeTypeAlias == \"StudentGroupsPage\""))
                {
                var itemName = node.HasValue("shortPageTitle") ? @node.shortPageTitle : node.HasValue("pageTitle") ? @node.pageTitle : @node.pageName;
                var levelOne = node.IsAncestor(Model) ? "class=levelOne" : "";
                var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                var target = node.externalUrl ? "target=_blank" : "";
                var link = node.HasValue("externalUrl") ? node.url : node.Url;
                <li @childActive @levelOne ><a href="@link" @target>@itemName</a>
        @if(node.Children.Where("Visible && NodeTypeAlias == \"Category2InternalPage\" || NodeTypeAlias == \"ExternalLink\" || NodeTypeAlias == \"StudentGroupsPage\"").Count() > 0 && node.IsAncestorOrSelf(Model) && node.Level <= FinishLevel)
              { 
              @traverse(node, StartLevel, FinishLevel);
              }
                </li>
              }
        </ul>     
    }
  • Steve Morgan 1278 posts 4216 karma points c-trib
    May 12, 2014 @ 17:11
    Steve Morgan
    0

    You're always going to hit your helper class (and therefore output your Unordered List tags as page will never be null as there always will be a self. 

    I do something similar using the CurrentPage object... you can use the Any() method - you could just wrap an if around what you already have if that works at the moment. 

     

    @if (CurrentPage.Children().Any()){

    <ul>

    .... <li> CODE HERE </li>

    }

  • Steve 472 posts 1216 karma points
    May 12, 2014 @ 18:19
    Steve
    0

    Thanks Steve, but I am using ver. 4.11 with webforms and I don't believe that I have that object class.

  • 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