Copied to clipboard

Flag this post as spam?

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


  • Marshmeryl 8 posts 109 karma points
    Apr 28, 2017 @ 13:52
    Marshmeryl
    0

    Cannot fetch media in Umbraco 7.5.3

    Hey,

    I've got a new install of Umbraco 7.5.3 and I keep getting an "Object reference not set to an instance of an object" error when I try to render media.

    Normally I fetch it the following way:

    var image = Model.Content.GetPropertyValue<IPublishedContent>("image");
    

    This doesn't work for some reason, but this does:

    var image = Umbraco.TypedMedia(1068);
    var imageUrl = image.GetPropertyValue<string>("umbracoFile");
    

    That doesn't help me much when I have to loop through images, though.

    Any ideas?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 28, 2017 @ 13:58
    Alex Skrypnyk
    0

    Hi Marshmeryl

    How do you want to loop through images? What datatype are you using?

    You can use this code in loop:

    var image = Umbraco.TypedMedia(1068);
    var imageUrl = image.GetPropertyValue<string>("umbracoFile");
    

    Can you show all you razor code and what exactly do you need?

    Thanks,

    Alex

  • Marshmeryl 8 posts 109 karma points
    Apr 28, 2017 @ 14:04
    Marshmeryl
    0

    Thanks for the fast response.

    This bit:

    var image = Model.Content.GetPropertyValue<IPublishedContent>("image");
    

    Doesn't work outside of a loop either :/

    Regarding the loops, here's what I do:

    I have nested content, which I fetch like this:

    var empathyMaps = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("empathyMap");
    

    Then:

    @if (empathyMaps != null)
    {
        foreach (var empathyMap in empathyMaps)
        {
            var personaName = empathyMap.GetPropertyValue<string>("personaName");
            var personaImage = empathyMap.GetPropertyValue<IPublishedContent>("personaImage");
    
            <img src="@personaImage.Url" alt="" class="em-persona">
        }
    }
    

    The string is fine, image throws the null reference error.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 28, 2017 @ 14:07
    Alex Skrypnyk
    0

    Try this code:

    @if (empathyMaps != null)
    {
        foreach (var empathyMap in empathyMaps)
        {
            var personaName = empathyMap.GetPropertyValue<string>("personaName");
    
            var personaImageId = empathyMap.GetPropertyValue<int>("personaImage");
    
            var personaImage = Umbraco.TypedMedia(personaImageId);
            if (personaImage != null)
            {
                <img src= "@personaImage.Url" alt ="" class="em-persona">
            }
        }
    }
    
  • Marshmeryl 8 posts 109 karma points
    Apr 28, 2017 @ 14:12
    Marshmeryl
    0

    Still null :/

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 28, 2017 @ 14:14
    Alex Skrypnyk
    0

    Is it right property alias - "personaImage"?

    Is it media picker property editor?

    Can you look at /app_data/umbraco.config - find needed node and look, please, how this field looks in xml?

    Thanks,

    Alex

  • Marshmeryl 8 posts 109 karma points
    Apr 28, 2017 @ 14:20
    Marshmeryl
    0

    It's the correct property alias and yes, media picker editor.

    Here's what the umbraco.config file says:

    enter image description here

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 28, 2017 @ 14:36
    Alex Skrypnyk
    0

    Interesting, and do you have media items with these ids in the system?

    Are you using "Umbraco-Core-Property-Value-Converters"?

    https://github.com/Jeavon/Umbraco-Core-Property-Value-Converters

  • Marshmeryl 8 posts 109 karma points
    Apr 28, 2017 @ 14:45
    Marshmeryl
    0

    Interesting, and do you have media items with these ids in the system?

    Yes, the two media items I have are exactly with those id's.

    Are you using "Umbraco-Core-Property-Value-Converters"?

    Also yes.

  • Marshmeryl 8 posts 109 karma points
    Apr 28, 2017 @ 15:15
    Marshmeryl
    101

    Ended up going in the Developer section and changing the data type from "Media Picker" to "Legacy Media Picker" and now it works...

    enter image description here

    Thanks for taking the time to try and help me!

  • Sven Geusens 169 posts 881 karma points c-trib
    Apr 28, 2017 @ 15:43
    Sven Geusens
    0

    A media picker returns an IEnumerable<IPublishedContent> when processed by Umbraco-Core-Property-Value-Converters. So change var personaImage = empathyMap.GetPropertyValue<IPublishedContent>("personaImage") by var personaImage = empathyMap.GetPropertyValue<IEnumerable<IPublishedContent>>("personaImage").FirstOrDefault()

Please Sign in or register to post replies

Write your reply to:

Draft