I am just starting to use Nested Content and have got my template working for my "level one" nested content. But I want to have nested content within nested content and I'm having trouble rendering the "second level" nested content on my template. Here's what I've done:
@inherits Umbraco.Web.Mvc.UmbracoViewPage
var items = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("sections");
foreach(var item in items)
{
<section class="section @item.GetPropertyValue("sectionClass")">
<div class="contain-content clearfix">
<article class="span-60
@if (item.HasValue("alignContent"))
{
var preValue = Umbraco.GetPreValueAsString(item.GetPropertyValue<int>("alignContent"));
@preValue.ToLower()
}
">
<h1>@item.GetPropertyValue("sectionTitle")</h1>
@item.GetPropertyValue("sectionContent")
@{
var secondItems = item.GetPropertyValue<IEnumerable<IPublishedContent>>("widget");
foreach(var secondItem in secondItems) {
}
}
</article>
</div>
</section>
}
I have a foreach loop for my first-level nested content (sections) which works fine, but then I've tried to add another foreach loop within that, to loop through my second-level nested content (widgets). This gives me an error: System.NullReferenceException: Object reference not set to an instance of an object.
(referring to the line that contains the second foreach loop)
Rendering Nested Content within Nested Content
I am just starting to use Nested Content and have got my template working for my "level one" nested content. But I want to have nested content within nested content and I'm having trouble rendering the "second level" nested content on my template. Here's what I've done:
I have a foreach loop for my first-level nested content (sections) which works fine, but then I've tried to add another foreach loop within that, to loop through my second-level nested content (widgets). This gives me an error: System.NullReferenceException: Object reference not set to an instance of an object. (referring to the line that contains the second foreach loop)
Can anyone tell me what I'm doing wrong?
Thanks in advance!
Is it as simple as?
I guess your concern is why is it null in the first place? I've had problems with this and adding new fields without publishing the content.
Thanks so much David,
That worked!
is working on a reply...