I am new to razor and I need to amend this code. My menu structure is:
about
==about 1
===about 1.1
==about 2
==about 3
currently when I'm on about it displays the children and grandchildren, but I want it to show the same menu if i'm on about 1,2,3 or about 1.1 which is level 4 as well.
This is the code adapted from show descendants of current page:
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@* Ensure that the Current Page has children, where the property umbracoNaviHide is not True @
@if (CurrentPage.Children.Where("Visible").Any())
{
@ Get the first page in the children, where the property umbracoNaviHide is not True *@
var naviLevel = CurrentPage.Children.Where("Visible").First().Level;
@* Add in level for a CSS hook *@
<ul class="level-@naviLevel">
@* For each child page under the root node, where the property umbracoNaviHide is not True *@
@foreach (var childPage in CurrentPage.AncestorOrSelf(1).Children.Where("Visible"))
{
<li>
<a href="@childPage.Url">@childPage.Name</a>
@* if the current page has any children, where the property umbracoNaviHide is not True *@
@if (childPage.Children.Where("Visible").Any())
{
@* Call our helper to display the children *@
@childPages(childPage.Children)
}
</li>
}
</ul>
}
@helper childPages(dynamic pages)
{
@* Ensure that we have a collection of pages @
if (pages.Any())
{
@ Get the first page in pages and get the level *@
var naviLevel = pages.First().Level;
@* Add in level for a CSS hook *@
<ul class="level-@(naviLevel)">
@foreach (var page in pages.Where("Visible"))
{
<li>
<a href="@page.Url">@page.Name</a>
@* if the current page has any children, where the property umbracoNaviHide is not True *@
@if (page.Children.Where("Visible").Any())
{
@* Call our helper to display the children *@
@childPages(page.Children)
}
</li>
}
</ul>
}
Help with Left Navigation
Hi there,
I am new to razor and I need to amend this code. My menu structure is:
about ==about 1 ===about 1.1 ==about 2 ==about 3
currently when I'm on about it displays the children and grandchildren, but I want it to show the same menu if i'm on about 1,2,3 or about 1.1 which is level 4 as well.
This is the code adapted from show descendants of current page: @inherits Umbraco.Web.Macros.PartialViewMacroPage
@* Ensure that the Current Page has children, where the property umbracoNaviHide is not True @ @if (CurrentPage.Children.Where("Visible").Any()) { @ Get the first page in the children, where the property umbracoNaviHide is not True *@ var naviLevel = CurrentPage.Children.Where("Visible").First().Level;
}
@helper childPages(dynamic pages) { @* Ensure that we have a collection of pages @ if (pages.Any()) { @ Get the first page in pages and get the level *@ var naviLevel = pages.First().Level;
}
The problem with this razor code is the first if statement.
If your current page doesn't have any children this won't display the menu.
I think you can solve it by determing the start item first.
Then update the if check
And the determining of the level :
And ofcourse your for each statement
Dave
Thank you Dave I got it working
Glad to see you got it working. Don't forget to mark the answer as a solution so others can find it as well.
Dave
Is it posible to activate the helper only when the upper level is active?
I'am new to razor and want to use it as Subnavigation.
You can set your startitem to any level you wish, and take children from that.
I Michael,
Thanks for your reply. I mean that u only see chlidren from active parents.
My structure in Umbraco is like:
At the front I only want to show Subitem 1.1 and Subitem 1.2 when Stubitem 1 is active.
When subitem 1 is active it doesn't have to show Subitems 2.1 and 2.2
is working on a reply...