Copied to clipboard

Flag this post as spam?

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


  • Nathan Rogan 22 posts 203 karma points
    Apr 21, 2020 @ 11:39
    Nathan Rogan
    0

    Multinode Treepicker - Value cannot be null

    Hi, I'm using the following to get MNTP values:

    @{ var selection = Model.Value<IEnumerable<IPublishedContent>>("tips").ToArray(); }
    
    @if (selection.Length > 0)
    {
        <ul>
            @foreach (var item in selection)
            {
                <li>
                    <a href="@item.Url">@item.Name</a>
                </li>
            }
        </ul>
    }
    

    If there's nothing picked I would expect the code not to run but it comes back with an error "System.ArgumentNullException: Value cannot be null." on the selection var. I have also tried .Count() but does the same thing.

    Has anyone got any suggestions?

    Thanks

  • Alex Skrypnyk 6168 posts 24148 karma points MVP 8x admin c-trib
    Apr 21, 2020 @ 12:02
    Alex Skrypnyk
    0

    Hi Nathan,

    If only one item has been selected in MNTP - then you have to use Model.Value

    Probably this code should work:

    @{ var selection = Model.Value<IEnumerable<IPublishedContent>>("tips").ToArray(); }
    
    @if (selection != null && selection.Length > 0)
    {
        <ul>
            @foreach (var item in selection)
            {
                <li>
                    <a href="@item.Url">@item.Name</a>
                </li>
            }
        </ul>
    }
    else
    {
        var selectedItem = Model.Value<IPublishedContent>("tips");
        if (selectedItem != null)
        {
            <ul>
                <li>
                    <a href="@selectedItem.Url">@selectedItem.Name</a>
                </li>
            </ul>
        }
    }
    

    Thanks,

    Alex

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Apr 21, 2020 @ 12:25
    Nik
    101

    The issue lies in this line:

    Model.Value<IEnumerable<IPublishedContent>>("tips").ToArray()
    

    The .ToArray() will throw an exception if nothing is picked because the .Value request returns Null if nothing is picked, not an empty IEnumerable. And that causes the problem you are seeing. So instead of checking count, you need to check for null.

    Thanks

    Nik

  • Alex Skrypnyk 6168 posts 24148 karma points MVP 8x admin c-trib
    Apr 21, 2020 @ 13:23
    Alex Skrypnyk
    0

    Thanks Nik

  • Nathan Rogan 22 posts 203 karma points
    Apr 21, 2020 @ 13:46
    Nathan Rogan
    0

    Thanks both for the quick reply. I have it working now using:

    @{ var selection = Model.Value<IEnumerable<IPublishedContent>>("tips"); }
    
    
    @if (selection != null)
    {
        <ul>
            @foreach (var item in selection)
            {
                <li>
                    <a href="@item.Url">@item.Name</a>
                </li>
            }
        </ul>
    }
    

    Cheers

Please Sign in or register to post replies

Write your reply to:

Draft