Copied to clipboard

Flag this post as spam?

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


  • Andy 9 posts 99 karma points
    Jun 01, 2020 @ 12:41
    Andy
    0

    Rendering nested content within another nested content

    Hi. Im working on an Umbraco 8.3 project where I need to add a Nested Content data type inside another Nested Content.

    I've done a lot of googling and I can only find examples for older Umbraco versions which don't seem to work in Umbraco 8.

    Here is the code i've got so far,

    @if (Model.HasValue("pageSections"))
    {
        var items = Model.Value<IEnumerable<IPublishedElement>>("pageSections");
        foreach(var item in items)
        {
            <h1>First level</h1>
            <div>
                @{
                    var secondItems = item.GetProperty("group").Value();
                    if(secondItems  != null) {
                        foreach(var secondItem in secondItems){
                            <p>Second level</p>
                        }
                    }
                }
            </div>
        }
    }
    

    Would be great if anyone has any ideas on what i'm doing wrong.

    Im not a developer so apologies if this is a silly question!

    Thanks!

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jun 01, 2020 @ 13:07
    Lee Kelleher
    100

    Hi Andy,

    Welcome to the forum! There are no silly questions here. :-)

    Try it the same way as how you got the first lot of items, like so...

    var secondItems = item.Value<IEnumerable<IPublishedElement>>("group");
    

    It's because the Razor template is expecting a certain object type, so it knows how to loop over it. In your original snippet, when you call .Value(), Razor doesn't know that it can be looped over, so it throws an error.

    Hopefully this should work for you.

    Cheers,
    - Lee

  • Andy 9 posts 99 karma points
    Jun 01, 2020 @ 13:13
    Andy
    0

    Hi Lee,

    Thanks for the quick reply. That works perfectly! It seems so obvious now.

    Thanks for your help!

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jun 01, 2020 @ 13:16
    Lee Kelleher
    0

    It's all a journey, one step at a time.

    Happy to hear that it worked!

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft