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>
}
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 ?
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.
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
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
Any assistance would be greatly appreciated
TIA Grant
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 theHasValue
method to check if a property exists on anIPublishedContent
object. Can you try the followingThanks
Paul
Hi Paul,
same error unfortunately
trying to get the group name then a list of Group sections below thanks
Grant
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
I think the following code includes checks for all possible null references, even though some are mandatory, give it a go...
if you place a break point in the code which part is falling over ? The property aliases are definitely correct ?
Thanks
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
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.
*code untested and needs to be on the groups razor template
Thanks
Paul
is working on a reply...