Copied to clipboard

Flag this post as spam?

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


  • blackhawk 313 posts 1368 karma points
    Nov 28, 2017 @ 19:32
    blackhawk
    0

    Multi Url Picker - Object reference not set on instance of object

    I am currently in a vanilla Umbraco 7.6.7 install, with the starter package.

    Install-Package RJP.UmbracoMultiUrlPicker -Version 2.1.0

    • Under the Settings Section, in my "content page" document type I created a Multi URL Picker property editor.

    enter image description here

    Under the Content Section, I created a new content node called my related links. And on that node, I filled some link values...

    enter image description here

    Within my Content Page Template I have the following code....

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.ContentPage>
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Umbraco.Web;
    @using Umbraco.Web.Models;
    @using RJP.MultiUrlPicker.Models;
    
    @ {
         Layout = "Master.cshtml";
     }
    
    @Html.Partial("~/Views/Partials/SectionHeader.cshtml")
    
    @ {
    
          var multiUrlPicker = Model.Content.GetPropertyValue<MultiUrls>("links");
    
          if (multiUrlPicker.Any())
          {
                <ul>
                    @foreach (var item in multiUrlPicker)
                    {
                        <li><a href="@item.Url" target="@item.Target">@item.Name</a></li>
                    }
                </ul>
            }
        }
    

    But this code generates the following error when the web page is loaded...

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    

    enter image description here

    I'm not sure what I am doing wrong in the process. Any ideas?

    Many thanks!

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 28, 2017 @ 21:56
    Alex Skrypnyk
    0

    Hi Blackhawk

    Try to use Related Links datatype instead of Multy Url Picker. Look how to use it - https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/Related-Links2

    Thanks,

    Alex

  • blackhawk 313 posts 1368 karma points
    Nov 28, 2017 @ 22:39
    blackhawk
    0

    I did earlier, and love it. The only thing that Related Links is lacking is the ability to link to documents stored in the Media section. I'm going to have editors wanting to link multiple documents onto any given content node. This is the "only" reason why I am messing with the Multi URL Picker.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Nov 29, 2017 @ 10:58
    Alex Skrypnyk
    0

    Hi, yes, I can understand you, I'm not the fan of RelatedLinks also, but it supported by the core

    Tyy to get links value as string and the cast it to "MultiUrls" manually

    Model.Content.GetPropertyValue<string>("links");
    

    Thanks,

    Alex

  • Ben Palmer 180 posts 866 karma points c-trib
    Nov 29, 2017 @ 10:56
    Ben Palmer
    100

    Hi blackhawk,

    What does Model.Content.GetPropertyValue("links") return?

    It depends on the version that you're using but that might be coming back as an `IEnumerable'.

    So, see what the above returns but you may also want to try Model.Content.GetPropertyValue<IEnumerable<Link>>("links") instead.

    You'll also want to change your if statement to:

    if (multiUrlPicker != null && multiUrlPicker.Any()) { }
    

    Hope that helps!

  • blackhawk 313 posts 1368 karma points
    Nov 29, 2017 @ 14:55
    blackhawk
    1

    Using Model.Content.GetPropertyValue<IEnumerable<Link>>("links"); saved the day!

    And I also learned the benefit of using@ObjectInfo to print out values. So using...

    @ObjectInfo.Print(multiUrlPicker)

    ....which is now displaying the correct values!

    Thank you Ben!

  • Martin 278 posts 662 karma points
    Nov 29, 2017 @ 12:10
    Martin
    1

    Hey Blackhawk,

    I had the same error. Make sure on the datatype you have either single or multi set.

    Single Link

    @{ 
        var singleLink = Model.Content.GetPropertyValue<Link>("singleLink");
    }
    
    @if(singleLink != null)
    {
      <a class="button" href="@singleLink.Url" target="@singleLink.Target">Explore</a>
    }
    

    MultiLink

    @{
        var multiLinks = Model.Content.GetPropertyValue<IEnumerable<Link>>("multiLinks");
    }
    @if(multiLinks.Any()){
        <ul>
            @foreach (var item in multiLinks)
            {
            <li><a href="@item.Url">@item.Name</li>
            }
        </ul>
    }  
    
  • blackhawk 313 posts 1368 karma points
    Nov 29, 2017 @ 14:56
    blackhawk
    0

    Thank you! This works, and thanks for the extra info on a single link.

Please Sign in or register to post replies

Write your reply to:

Draft