Copied to clipboard

Flag this post as spam?

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


  • DonZalmrol 220 posts 833 karma points
    Feb 22, 2016 @ 10:41
    DonZalmrol
    0

    Show replacement image if no image has been found

    Hi,

    I wish to add a default image if no project image has been uploaded in my backend.

    I have this piece of code:

    @if(Model.Content.HasValue("@item.pictureOfTheProject"))
    {
        var featureImage = Umbraco.TypedMedia((int)item.pictureOfTheProject); 
        <img class="img-thumbnail" src="@featureImage.GetCropUrl(200, 200)" alt="@item.titleOfTheProject" />    
    }
    
    else
    {
        var noImage = "http://www.don-zalmrol.be/media/1439/no-thumb.png";
        <img class="img-thumbnail" src="@noImage.GetCropUrl(200, 200)" alt="No image" />
    }
    

    But now it's only showing me the replacement photo, instead of one uploaded image for a project and one for a project that has no image.

    I'm testing this on my personal site: http://www.don-zalmrol.be/brewing/

    Thanks!

  • suzyb 474 posts 932 karma points
    Feb 22, 2016 @ 10:56
    suzyb
    1

    If you are getting the properties of "item" then does your if not need to be

    @if (item.HasValue("pictureOfTheProject"))
    
  • Jeavon Leopold 3074 posts 13631 karma points MVP 11x admin c-trib
    Feb 22, 2016 @ 10:58
    Jeavon Leopold
    100

    Your if is not correct, I think it should be:

    @if(item.HasValue("pictureOfTheProject"))

  • DonZalmrol 220 posts 833 karma points
    Feb 22, 2016 @ 11:08
    DonZalmrol
    0

    All be damned!

    Both of you are correct. Now it's working! Many thanks!

    This will now also resolve the nullref error I get when nothing is added :)

Please Sign in or register to post replies

Write your reply to:

Draft