Copied to clipboard

Flag this post as spam?

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


  • DonZalmrol 208 posts 811 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 464 posts 877 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 3008 posts 13221 karma points MVP 7x 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 208 posts 811 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 :)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies