Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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:
var homePgID = 1065;
var hours = Umbraco.Content(homePgID).Value<IEnumerable<IPublishedContent>>("busHours").ToList();
The reason you're getting a null exception is because you're returning IPublishedContent, instead of IPublishedElement.
Something like the following, should work:
var hours = Umbraco.Content(homePgID).Value<IEnumerable<IPublishedElement>>("busHours").ToList();
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 of IPublishedElement entities (multiple items).
Awesome, thank you very much for your help!
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted