Just using Nested Content for first time. All works fine except when nothing has been entered by the editor. I get YSOD about Value cannot be null.
Parameter name: source on the "@if" line below:-
var straplines = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("Straplines");
@if(straplines.Count() > 0) {
foreach (var item in straplines) {
<li>'@item.GetPropertyValue("strapline")' <span class="accreditation">@item.GetPropertyValue("attribution")</span></li>
}
}
Can't check for .HasValue or .HasProperty, or .IsNull as the IEnumerable doesn't allow it, which is why I tried "Count()" as "Any()" didn't work either.
Sorry about that... if I was developing it today, I'd have return an Enumerable.Empty<IPublishedContent>. It would have saved on the null reference check. Coding is a continual learning curve.
How to test for empty Nested Content collection
Just using Nested Content for first time. All works fine except when nothing has been entered by the editor. I get YSOD about
Value cannot be null. Parameter name: source
on the "@if" line below:-Can't check for .HasValue or .HasProperty, or .IsNull as the IEnumerable doesn't allow it, which is why I tried "Count()" as "Any()" didn't work either.
Any advice would be appreciated.
Thanks,
Craig
Hi Craig,
I assume you've tried a null check?
Cheers,
- Lee
I've just checked the Nested Content source-code, it does return a
null
if the value is empty.https://github.com/umco/umbraco-nested-content/blob/develop/src/Our.Umbraco.NestedContent/Extensions/PublishedPropertyTypeExtensions.cs#L114
Sorry about that... if I was developing it today, I'd have return an
Enumerable.Empty<IPublishedContent>
. It would have saved on the null reference check. Coding is a continual learning curve.Thanks Lee,
No, hadn't tried that, but have now and it works :)
Cheers,
Craig
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.