Copied to clipboard

Flag this post as spam?

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


  • Rizwan 28 posts 138 karma points
    Aug 04, 2022 @ 09:42
    Rizwan
    0

    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 2141 posts 14344 karma points MVP 8x c-trib
    Aug 07, 2022 @ 09:44
    Marc Goodson
    100

    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){
    <h2>@author.Name</h2>
    @author.Value<IHtmlString>("biog")
    }
    }
    

    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")){
    <h2>@author.Name</h2>
    @author.Value<IHtmlString>("biog")
    }
    }
    

    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!

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft