Copied to clipboard

Flag this post as spam?

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


  • Ken Burcham 9 posts 100 karma points c-trib
    Sep 17, 2020 @ 15:15
    Ken Burcham
    0

    looping Link items in a Umbraco.NestedContent causes NullReferenceException if the link's target page is deleted

    Hi there!

    I'm on Umbraco 8.5.3.

    I have a composition called "Related Link List" for editors to add a list of links to a page. I used a Umbraco.NestedContent property and configured it with a "Link item" element that is an "Umbraco.MultiUrlPicker" configured to allow only 1 URL.

    Normally this works great. I iterate the list and display the list o' links on a sidebar.

    However, if the user makes a link to a page on the site and then deletes the page, I get a NullReferenceException that blows up the page.

    @inherits UmbracoViewPage
    
    @using Umbraco.Web.Models
    
    @{
    var relatedItems = Model.Value<IEnumerable<IPublishedElement>>("relatedLinkList");
    }
    
    <div class="right-sidebar">
    
    @Html.Partial("~/Views/Partials/deptContactBox.cshtml")
    
    @if (relatedItems != null && relatedItems.Any())
    {
        <h4 class="related-items-header">Related Items</h4>
        <div class="sidebar-related-items">
            @foreach (var item in relatedItems)
            {
                var link = item.Value<Link>("linkURL");
    
                <div class="sidebar-related-item">
                    <a href="@link.Url">@link.Name</a>
                </div>
            }
        </div>
    }
    </div>
    

    The error is thrown on this line:

    var link = item.Value<Link>("linkURL");
    

    I expect there is something in the Link construction that isn't checking to see if the target is trashed before trying to render.

    What I'd like to do (I think) is test before trying to construct the link but I'm not sure how to do it; neither HasProperty or HasValue returns a false. Or maybe there's a better way? I can also wrap the thing in a try catch, but that seems not so elegant. :)

    Is there a way to cast something so that I can test for isTrashed or something I can add to the linq query to only return valid links?

    Thank you!

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Sep 17, 2020 @ 17:03
    Nik
    100

    Hey Ken,

    Razor often lies about which line the error is on.

    Try this:

    @foreach (var item in relatedItems)
    {
       var link = item.Value<Link>("linkURL");
       if(link != null)
       {
           <div class="sidebar-related-item">
               <a href="@link.Url">@link.Name</a>
           </div>
       }
    }
    

    Thanks

    Nik

  • Ken Burcham 9 posts 100 karma points c-trib
    Sep 17, 2020 @ 17:57
    Ken Burcham
    1

    I didn't realize that razor lies! :)

    Yes, that totally did it. Thank you!

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Sep 17, 2020 @ 18:17
    Nik
    0

    Yeah, if you get a Null reference exception in a razor file, look for the next razor usage of the object in question from the line you've been taken too. That's normally where the exception is actually been thrown tbh.

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft