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.
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.
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> }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>
}
Thanks Steve, but I am using ver. 4.11 with webforms and I don't believe that I have that object class.
is working on a reply...
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.