Umbraco 8 Nested Content always returns null value
I'm grabbing a nested content item from the home page and assigning it to a variable. I know that I've added values to the item in the back end, but no matter what I try it always returns null. I'm using Umbraco v 8.0.2.
This is my code:
// hours
var homePgID = 1065;
var hours = Umbraco.Content(homePgID).Value<IEnumerable<IPublishedContent>>("busHours").ToList();
Thanks Rhys! That seems to have worked. Would changing it to IPublishedElement affect it negatively if there were more than one item in the nested content?
Yeah - if you only type IPublishedElement when trying to return multiple items, you'll most likely encounter issues.
By default, I would always recommend that you use IEnumerable<IPublishedElement> since most Nested Content will likely be trying to retrieve a list of IPublishedElement entities (multiple items).
Umbraco 8 Nested Content always returns null value
I'm grabbing a nested content item from the home page and assigning it to a variable. I know that I've added values to the item in the back end, but no matter what I try it always returns null. I'm using Umbraco v 8.0.2.
This is my code:
The reason you're getting a null exception is because you're returning
IPublishedContent
, instead ofIPublishedElement
.Something like the following, should work:
You can find out more about how to use Nested Content here.
Thanks Rhys! That seems to have worked. Would changing it to IPublishedElement affect it negatively if there were more than one item in the nested content?
Glad that sorted it!
Yeah - if you only type
IPublishedElement
when trying to return multiple items, you'll most likely encounter issues.By default, I would always recommend that you use
IEnumerable<IPublishedElement>
since most Nested Content will likely be trying to retrieve a list ofIPublishedElement
entities (multiple items).Awesome, thank you very much for your help!
is working on a reply...