Copied to clipboard

Flag this post as spam?

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


  • Grant Stephen 4 posts 84 karma points
    Mar 06, 2023 @ 12:04
    Grant Stephen
    0

    Nested Content on Parent

    Hi, looking to pull nested content from a child and render on parent page.

    tried different ways but always

    object reference not set to an instance of an object error

    currently site is umbraco 8

       @{
        var selection = @Model.Children;
    
        <ul>
            @if (selection.Any())
            {
                foreach (var item in selection)
                {
                    <li>@item.Name</li>
                    foreach (var groupSection in item.Value<IEnumerable<IPublishedContent>>("groupSections"))
                    {
                        <li>
                            @groupSection.Value("groupSectionName")
                        </li>
                    }
                }
            }
        </ul>
    }
    

    Any assistance would be greatly appreciated

    TIA Grant

  • Paul Griffiths 370 posts 1021 karma points
    Mar 06, 2023 @ 12:30
    Paul Griffiths
    0

    Hi Grant,

    Without knowing the complete structure of your site and the available properties on your doc types then what I suggest might not be completely accurate. However, based on your example code it looks like you are trying to access a nested property called groupSections which should be defined on each child item in the @Model.Children collection ?

    It might be possible that some of those child items don't have that property which could lead to the "object reference not set to an instance of an object" error.

    It might be worth defending against this and checking if the groupSections property exists on each item before trying to access it. I think you can use the HasValue method to check if a property exists on an IPublishedContent object. Can you try the following

        @{
        var selection = Model.Children;
    
        <ul>
            @if (selection.Any())
            {
                foreach (var item in selection)
                {
                    <li>@item.Name</li>
                    if (item.HasValue("groupSections"))
                    {
                        var groupSections = item.Value<IEnumerable<IPublishedContent>>("groupSections");
                        foreach (var groupSection in groupSections)
                        {
                            <li>@groupSection.Value("groupSectionName")</li>
                        }
                    }
                }
            }
        </ul>
    }
    

    Thanks

    Paul

  • Grant Stephen 4 posts 84 karma points
    Mar 06, 2023 @ 13:05
    Grant Stephen
    0

    Hi Paul,

    same error unfortunately

    site structure

    trying to get the group name then a list of Group sections below thanks

    Grant

  • Paul Griffiths 370 posts 1021 karma points
    Mar 06, 2023 @ 13:16
    Paul Griffiths
    0

    Hi Grant,

    So on the 5th Dundee (Downfield) Scout Group page you would like to display 5th Dundee (Downfield) Squirrel Drey, 5th Dundee (Downfield) clubs etc? Structure is something like

    • 5th Dundee (Downfield) Scout Group (parent)
      • 5th Dundee (Downfield) Squirrel Drey (nested on parent page)
      • 5th Dundee (Downfield) clubs (nested on parent page)

    I think the following code includes checks for all possible null references, even though some are mandatory, give it a go...

    @if (Model != null && Model.Children != null)
    {
        var selection = Model.Children;
    
    <ul>
        @if (selection != null && selection.Any())
        {
            foreach (var item in selection)
            {
                if (item != null)
                {
                    <li>@item.Name</li>
                    if (item.HasValue("groupSections"))
                    {
                        var groupSections = item.Value<IEnumerable<IPublishedContent>>("groupSections");
                        if (groupSections != null)
                        {
                            foreach (var groupSection in groupSections)
                            {
                                if (groupSection != null)
                                {
                                    <li>@groupSection.Value("groupSectionName")</li>
                                }
                            }
                        }
                    }
                }
            }
        }
    </ul>
    }
    

    if you place a break point in the code which part is falling over ? The property aliases are definitely correct ?

    Thanks

  • Grant Stephen 4 posts 84 karma points
    Mar 06, 2023 @ 13:40
    Grant Stephen
    0

    Hi Paul the error has gone but ideally id like to render a groups page that will show all groups with their corresponding group sections each of them will link to a group specific page.

    Thanks

    Grant

  • Paul Griffiths 370 posts 1021 karma points
    Mar 06, 2023 @ 13:56
    Paul Griffiths
    0

    Hi Grant,

    Good to hear. You are probably going to need a nested foreach, one to loop through the children of the groups page and then another to loop through the nested content that is defined on the group children.

        @if (Model != null && Model.Children != null)
    {
        var childGroups = Model.Children.Where(x => x.Level == 2);
    
        <ul>
            @if (childGroups != null && childGroups.Any())
            {
                foreach (var item in childGroups)
                {
                    if (item != null)
                    {
                        <li>@item.Name</li>
    
                        //process nested content on the group
                        if (item.HasValue("groupSections"))
                        {
                            var groupSections = item.Value<IEnumerable<IPublishedContent>>("groupSections");
                            if (groupSections != null && groupSections.Any())
                            {
                                foreach (var groupSection in groupSections)
                                {
                                    if (groupSection != null)
                                    {
                                        <li>@groupSection.Value("groupSectionName")</li>
                                    }
                                }
                            }
                        }
                    }
                }
            }
        </ul>
    }
    

    *code untested and needs to be on the groups razor template

    Thanks

    Paul

Please Sign in or register to post replies

Write your reply to:

Draft