Copied to clipboard

Flag this post as spam?

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


  • bh 298 posts 1015 karma points
    1 week ago
    bh
    0

    ImageCropper MediaItem thinks it's null

    var fbimage = Model.Value("FacebookImage") == null ? null : Model.Value<IPublishedContent>("FacebookImage").GetCropUrl(1200,630);
    

    Model.Value("FacebookImage") is not null, but I get the below error on the line of code above.

    Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: mediaItem

    What am I missing?

  • Dan Diplo 1524 posts 6000 karma points MVP 4x c-trib
    1 week ago
    Dan Diplo
    0

    What version of Umbraco are you using?

  • bh 298 posts 1015 karma points
    1 week ago
    bh
    0

    8.14

  • Dan Diplo 1524 posts 6000 karma points MVP 4x c-trib
    1 week ago
    Dan Diplo
    1

    If you are using the new Media Picker 3 then it no longer returns IPublishedContent but, instead, a new type called MediaWithCrops that then has a property called MediaItem that contains the actual published media content.

    So you'd need to change your code to be something like this (not 100% sure):

    var fbimage = Model.Value("FacebookImage") == null ? null : Model.Value<MediaWithCrops>("FacebookImage").GetCropUrl(1200,630);

    I've raised this as an issue as it feels like a breaking change, but that is the way it is now. See https://github.com/umbraco/Umbraco-CMS/issues/10397 for the back story.

  • bh 298 posts 1015 karma points
    1 week ago
    bh
    0

    Thanks for the info. Unfortunately MediaWithCrops doesn't have GetCropUrl. I despise crop aliases! I much prefer specifying the crop in code. I started out using cropaliases in Umbraco, and adding/maintaining them was so much more laborious than just specifying .GetCropUrl(W,H) when adding an image to my template.

  • bh 298 posts 1015 karma points
    1 week ago
    bh
    101

    @DanDiplo you were VERY close. Thanks for pointing me in the right direction!!

    This works:

    var fbimage = Model.Value("FacebookImage") == null ? null : Model.Value<MediaWithCrops>("FacebookImage").MediaItem.GetCropUrl(1200,630);
    
Please Sign in or register to post replies

Write your reply to:

Draft