Alistair67
Alistair67

Reputation: 59

Umbraco Razor get children of a child

I'm trying to build a menu with submenus using razor, which I'm not familiar with, and so far I can get the elements of the top level with:

@{ var selection = CurrentPage.Site().Children.Where("Visible"); }

and then:

@foreach (var item in selection)
{
    <li class="@(item.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
        <a href="@item.Url">@item.Name</a>
    </li>
 }

However, if I try to test if the next level exists with:

@foreach (var item in selection)
{
    <li class="@(item.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
        <a href="@item.Url">@item.Name</a>
    </li>

    @if (item.Children.Where("Visible").Any()) { <li><p>has children</p></li> } 

}

I get an error.

@if (item.Descendants().Where("Visible").Any()) { <li><p>has children</p></li> }

This doesn't work either.

Can anyone enlighten me as to what I'm doing wrong? I assume it is possible somehow but googling the problem hasn't thrown anything up yet.

Upvotes: 0

Views: 708

Answers (1)

Vinay Sud
Vinay Sud

Reputation: 26

This must be a Parser Error. You've already declared "@" at the beginning of your foreach loop, therefore you don't need it before your "if" statement.

Change this:

@if (item.Children.Where("Visible").Any()) { <li><p>has children</p></li> }

to this:

if (item.Children.Where("Visible").Any()) { <li><p>has children</p></li> }

Upvotes: 1

Related Questions