Copied to clipboard

Flag this post as spam?

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


  • darzren 4 posts 83 karma points
    Feb 09, 2020 @ 07:17
    darzren
    0

    Get Image from Media Picker

    Hi,

    Please help.

    I am getting error - Object reference not set to an instance of an object.' when using below code.

    I am using Umbraco 8 and this code is on my contentpage.cshtml

     <div class="logo">
           <a href="#" class="logo">
       <img src="@Umbraco.Media(@Umbraco.AssignedContentItem.GetProperty("sitelogo").GetValue()).Url" /> 
                       </a>
    
  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Feb 09, 2020 @ 09:30
    Marc Goodson
    101

    Hi darzren

    I'm wondering if something like the following might help work out what's going wrong here:

    @{
    var siteLogo = Model.Value<IPublishedContent>("siteLogo");
    bool hasSiteLogo = siteLogo!=null;
    }
    @if (hasSiteLogo){
     <div class="logo">
           <a href="#" class="logo">
       <img src="@siteLogo.Url" /> 
                       </a>
    </div>
    }else {
    

    }

    By first checking whether the sitelogo is null before trying to write out the Url property, will give you the avenue to investigate, eg is the page with the siteLogo published? is the image in the media section etc...

    Also, is the SiteLogo on the top root node of the site? but this code is written out on all pages? in which case the SiteLogo property won't exist on other pages... which might be the cause of the error - the way to workaround this is to read the property recursively up the ancestors of the tree until the sitelogo property is found eg:

    @{
    var siteLogo = Model.Value<IPublishedContent>("siteLogo", fallback: Fallback.ToAncestors);
    bool hasSiteLogo = siteLogo!=null;
    }
    @if (hasSiteLogo){
     <div class="logo">
           <a href="#" class="logo">
       <img src="@siteLogo.Url" /> 
                       </a>
    </div>
    }else {
    

    }

    But hopefully this makes it easier to debug!

    regards

    Marc

  • darzren 4 posts 83 karma points
    Feb 10, 2020 @ 09:55
    darzren
    0

    Hi Marc,

    Thanks. It is working now.

    my logo not on my root node. It is on one of my content page. I will work on the home page later on and check for null as well

    Btw, can you explain why this Model.Value

    As for render text, i am using Model.Value("sitename") and it works.

    Thanks.

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Feb 10, 2020 @ 18:13
    Marc Goodson
    0

    Hi darzren

    If I understand your follow up question correctly (I think the <> have been removed)

    Then when you write out properties from Umbraco, say a text string and you use

    @Model.Value("aliasofProperty")
    

    Then Umbraco 'guesses' what type of property it is and writes out the value... for a text field this is ok, becaues the value stored is the text you want to display...

    ... for a picker, like a content picker or media picker, the value stored is actually just the unique Id of the item that has been picked... and you 'could' use this along with an Umbraco Helper to retrieve the full picked item and retrieve the url...

    eg

    var pickedImage = Umbraco.Media(Model.Value("aliasOfImagePicker"));
    

    but you end up 'doing this' all over the place in a view, which might have multiple images, or indeed a property that stores it's data in a more complex way than just text or the id... and so... PropertyValueConverters were born!

    These secretly hide away in the background, (you can create your own) and are wired up to work with certain property types, you trigger them when you ask for the content in a specific type, via the <T> brackets after Model.Value ... if there is a PropertyValueConverter than knows how to take the stored value for that property and turn it into the type you are requesting, it will do the magic for you... so

    var pickedImage = Model.Value<IPublishedContent>("aliasofImagePicker");
    

    has the same outcome of the above, because there is a PropertyValueConverter 'under the hood' which knows how to convert from the picked media id into the Media object represented by IPublishedContent - and hence has the .Url property...

    For Multiple Media Picker you would use:

    var pickedImages = Model.Value<IEnumerable<IPublishedContent>>("aliasofImagesPicker");
    

    and for a Multi Url Picker something like this

    var footerLinks = Model.Value<IEnumerable<Link>>("aliasofFooterLinksUrlPicker");
    

    So hopefully makes it easier to work with in the view...

    if that was your question :-)

    regards

    Marc

  • Brendan 3 posts 72 karma points
    Mar 17, 2022 @ 08:42
    Brendan
    0

    Thanks this helped allot for Umbraco 9.

    For anyone who has created a partial and wants to simply get the url of the media picker:

    In your partial:

    @{

    var desktopImage = Model.Value

    }

  • Brendan 3 posts 72 karma points
    Mar 17, 2022 @ 09:11
    Brendan
    0

    You also just want to check if your media picker has an image like this:

    var desktopImage = Model.Value

    Look for a url if it doesn't exist set your variable to an empty string otherwise your page will not render..

Please Sign in or register to post replies

Write your reply to:

Draft