Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig100 1078 posts 2366 karma points c-trib
    Oct 13, 2016 @ 11:25
    Craig100
    0

    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:-

    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.

    Any advice would be appreciated.

    Thanks,

    Craig

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Oct 13, 2016 @ 14:26
    Lee Kelleher
    100

    Hi Craig,

    I assume you've tried a null check?

    if (straplines != null && straplines.Any())
    

    Cheers,
    - Lee

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Oct 13, 2016 @ 14:43
    Lee Kelleher
    0

    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.

  • Craig100 1078 posts 2366 karma points c-trib
    Oct 13, 2016 @ 14:46
    Craig100
    0

    Thanks Lee,

    No, hadn't tried that, but have now and it works :)

    Cheers,

    Craig

  • 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.

Please Sign in or register to post replies