Computer Magic Logo
Children of a page

Saturday, August 8, 2015

Published by Aristotelis Pitaridis

We can use the Children property of a page in order to get a list of all the children pages of a page. Let’s see how to do it using a typed representation for the current page.

<ul>
    @foreach (var item in Model.Content.Children)
    {
        <li>@item.Name</li>
    }
</ul>

The equivalent code for getting the child pages using the dynamic representation of the page is the following.

<ul>
    @foreach (var item in CurrentPage.Children)
    {
        <li>@item.Name</li>
    }
</ul>

Sometimes we may have to check if there are available child pages in order to display a message if there are not child pages. Let’s see how to do it using a typed representation of the page.

@if (Model.Content.Children.Count() > 0)
{
    <ul>
        @foreach (var item in Model.Content.Children)
        {
            <li>@item.Name</li>
        }
    </ul>
}
else
{
    <p>There are not child pages.</p>
}

The equivalent code using the dynamic representation of the page is the following.

@if (CurrentPage.Children.Count() > 0)
{
    <ul>
        @foreach (var item in CurrentPage.Children)
        {
            <li>@item.Name</li>
        }
    </ul>
}
else
{
    <p>There are not child pages.</p>
}