Copied to clipboard

Flag this post as spam?

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


  • Tom 3 posts 23 karma points
    Jun 18, 2012 @ 22:13
    Tom
    0

    Test if image exists, and display if it does

    Hello,

    This might sound easy but I can't get it to work.

    In my document type I have defined a footerImage (of type Uploader) which is optional.

    In my template I want to try to display the image only if it exists.

    I know I need some sort of if statement around the below line and I have tried many and all fail. What is the simple solution to this problem.

    <img src="@Umbraco.GetMediaUrl(Model.Id, "footerPhoto")" alt="@CurrentPage.Name" />

    I am using Umbraco 5.
    Regards,

    Tom

  • Halldór Hrafn 15 posts 72 karma points
    Jun 19, 2012 @ 00:27
    Halldór Hrafn
    0

    See if this one (or something along these lines) suits you:

    ... works in v4.7.2

    if (Model.HasProperty("footerPhoto")) {
        <img src="@(Model.Media("footerPhoto").UmbracoFile)" />
    }
  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 19, 2012 @ 04:29
    Fuji Kusaka
    0

    Hi Tom.

     You could do something like this to check if image has been uploaded

    @{
          if(String.IsNullOrEmpty(@Model.footerPhoto.ToString()) == false){
            <img src="@Model.footerPhoto" title="@Model.Name"/>
            }
        
          else{
                 <div>Nothing</div>
               }
      }

     

    //fuji

  • Tom 3 posts 23 karma points
    Jun 19, 2012 @ 16:30
    Tom
    0

    Thanks for the two replies.


    Neither works.

    Halldór Hrafn -

    Compiler Error Message: CS1061: 'Umbraco.Cms.Web.Model.Content' does not contain a definition for 'HasProperty' and no extension method 'HasProperty' accepting a first argument of type 'Umbraco.Cms.Web.Model.Content' could be found (are you missing a using directive or an assembly reference?)
    Source Error:

     
    Line 32: @{
    Line 33: if (Model.HasProperty("footerPhoto")) {

     

     

     

    Fuji -

    Compiler Error Message: CS1061: 'Umbraco.Cms.Web.Model.Content' does not contain a definition for 'footerPhoto' and no extension method 'footerPhoto' accepting a first argument of type 'Umbraco.Cms.Web.Model.Content' could be found (are you missing a using directive or an assembly reference?)
    Source Error:

     
    Line 32: @{
    Line 33: if(String.IsNullOrEmpty(@Model.footerPhoto.ToString()) == false){

     

     

     

     

     

     

  • Douglas Ludlow 210 posts 366 karma points
    Jun 19, 2012 @ 16:56
    Douglas Ludlow
    0

    It appears that you are using Umbraco 5 and both of those solutions are for Umbraco 4.7.

  • Tom 3 posts 23 karma points
    Jun 19, 2012 @ 17:19
    Tom
    0

    Yes, Umbraco 5

Please Sign in or register to post replies

Write your reply to:

Draft