Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Rizwan 25 posts 125 karma points
    1 week ago

    NestedContent From Child Page in Parent

    Dear Experts,

    I want to show the nested content of child pages in parent page. i am able to get the child pages as below. @foreach(var child in Model.Children){

    //i can get the child page name

    //here i need to access nestedcontent and loop for the items and display here }

    Please advice

  • Marc Goodson 1718 posts 11412 karma points MVP 7x c-trib
    5 days ago
    Marc Goodson

    Hi Rizwan

    There is a couple of ways you can go with this and it depends on the type of pages the 'children' are.

    IF all your children of the parent page are of the same type, let me say they are all of 'BlogPost' type and have a NestedContent property called 'Authors' then you could change your foreach loop to be

    @foreach (var child in Model.Children().OfType<BlogPost>()){
    foreach (var author in child.Authors){

    The 'OfType' is casting all the children of the page to be of the generated Modelsbuilder type 'BlogPost' and an underlying PropertyValueConverter has helpfully turned the NestedContent property into a collection of Authors you can loop through.

    If you have 'different' types of pages underneath the parent, then you could either make sure the different types of pages implement a common interface, or you could work with what .Children returns by default, eg an IEnumerable of IPublishedContent elements, it would look something like this:

    @foreach (var child in Model.Children){
    foreach (var author in child.Value<IEnumerable<IPublishedContent>>("authors")){

    You can see without 'OfType' we are having to fish for the properties by 'alias', so there is more to type, and more chance of typos!



Please Sign in or register to post replies

Write your reply to: