I'm trying to access propertis of some nested content but am having issues.
I can loop through the content and display 'Name' but can't access any of the properties of each item.
I have properties:
title, intro, image
and have tried:
@foreach (var page in CurrentPage.Site().Children.Where("Visible"))
{
var items = page.GetPropertyValue<IEnumerable<IPublishedContent>>("sectionPanels");
if (items != null)
{
@foreach (var child in items)
{
<a href="">@child.Name</a></li>
<a href="">@child.GetPropertyValue("title")</a></li>
}
}
}
As i said it will get the value for @child.Name but I get the error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''Umbraco.Web.PublishedContentModels.ServiceProgramsSectionPanel' does not contain a definition for 'GetPropertyValue''
When trying to access @child.GetPropertyValue("title")
As you've got Models builder enabled, you can create a typed model of your nested content.
Make your foreach something like this:
foreach(var rawChild in items)
{
var child = new ServiceProgramsSectionPanel(rawChild);
<a href="">@child.Title</a></li>
}
I find doing this makes things easier.
As Dave mentions, Nested content isn't a full content item. It is what's called a disconnected content type (I think). That doesn't explain why the GetPropertyValue method isn't working, it works for me normally. But if you use a Typed Model, it works around it I find.
Get property values in Nested Content
I'm trying to access propertis of some nested content but am having issues.
I can loop through the content and display 'Name' but can't access any of the properties of each item.
I have properties: title, intro, image
and have tried:
As i said it will get the value for @child.Name but I get the error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''Umbraco.Web.PublishedContentModels.ServiceProgramsSectionPanel' does not contain a definition for 'GetPropertyValue''
When trying to access @child.GetPropertyValue("title")
When debugging I see this for @child:
Hi James,
This is because the a nested content item is not a real content item and therefore has no name.
Dave
Hi James,
As you've got Models builder enabled, you can create a typed model of your nested content.
Make your foreach something like this:
I find doing this makes things easier.
As Dave mentions, Nested content isn't a full content item. It is what's called a disconnected content type (I think). That doesn't explain why the GetPropertyValue method isn't working, it works for me normally. But if you use a Typed Model, it works around it I find.
Nik
is working on a reply...