Copied to clipboard

Flag this post as spam?

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


  • mikkel 143 posts 365 karma points
    Mar 01, 2019 @ 08:57
    mikkel
    0

    why does my check for image exist not working

    Hi every one 'i am tryimg to check if my image exist and if it dont i want to insert a default image. i have lookt arount the forum but i cant get the examples to work on my solution.

    Here is what i have try to do, what am i doing wrong

    @{
       if (Model.Content.GetPropertyValue("billedeProjekt") != null)
              {
                 <img src="@Umbraco.TypedMedia(item.GetPropertyValue<int>("billedeProjekt")).Url" style="width: 100%; height: 262px;" />
              }
               else
                {
                     <h1>test</h1>
                }    
       }
    
  • Rhys Hamilton 140 posts 942 karma points
    Mar 01, 2019 @ 09:46
    Rhys Hamilton
    0

    Hi Mikkel,

    The Umbraco.TypedMedia only accepts an int parameter. Essentially, you're supplying the model first and then returning the property as an int.

    As soon as it sees the Model part, it'll throw the null exception regardless (at least from my experience).

    I've put together a solution that works, which should resolve your issue and reduce the need for multiple if/else statements. This should never return null, even if your property doesn't exist.

    Code below:

    @{
        // If billedeProjekt does not exist, set the default id to 0, otherwise, get the proper id
        var billedeProjekt = Model.Content.HasProperty("billedeProjekt") ? Model.Content.GetPropertyValue<int>("billedeProjekt") : 0;
    
        // If the billedeProjekt is 0, use your default placeholder image (e.g - http://via.placeholder.com/150x150)
        // Otherwise, grab the URL
        var image = billedeProjekt == 0 ? "http://via.placeholder.com/150x150" : Umbraco.TypedMedia(billedeProjekt).Url;
    
        <img src="@image" style="width: 100%; height: 262px" />
    }
    
  • mikkel 143 posts 365 karma points
    Mar 01, 2019 @ 12:27
    mikkel
    0

    You mean something like this ?

       / / If billedeProjekt does not exist, set the default id to 0, otherwise, get the proper id
                var billedeProjekt = Model.Content.HasProperty("billedeProjekt") ? Model.Content.GetPropertyValue<int>("billedeProjekt") : 0;
                if(billedeProjekt == null)
                {
                 // If the billedeProjekt is 0, use your default placeholder image (e.g - http://via.placeholder.com/150x150)
                    // Otherwise, grab the URL
                    var image = billedeProjekt == 0 ? "http://via.placeholder.com/150x150" : Umbraco.TypedMedia(billedeProjekt).Url;
                }
                else{
    
                    <img src="@image" style="width: 100%; height: 262px" />
                }
    
  • mikkel 143 posts 365 karma points
    Mar 01, 2019 @ 13:08
    mikkel
    1

    i have solved it myself. i didt it with this ode here

    @try
    {    
        if (item.GetPropertyValue<int>("billedeProjekt") != null)
        {          
          <img src="@Umbraco.TypedMedia(item.GetPropertyValue<int>("billedeProjekt")).Url" style="width: 100%; height: 262px;" />       
        }
    }
    catch (Exception e)
    {
      <img src="~/Media/standard-image/billede-på-vej.png" style="width: 100%; height: 262px;"/>
    }
    
  • Rhys Hamilton 140 posts 942 karma points
    Mar 01, 2019 @ 16:48
    Rhys Hamilton
    1

    Hi Mikkel,

    I think the comments in my code might have caused some confusion (they were just there to describe what each line was doing). The code I'd supplied removes the need for if/else or try/catch.

    Without the comments, the full code would look like so:

    @{    
        var billedeProjekt = Model.Content.HasProperty("billedeProjekt") ? Model.Content.GetPropertyValue<int>("billedeProjekt") : 0;
        var image = billedeProjekt == 0 ? "http://via.placeholder.com/150x150" : Umbraco.TypedMedia(billedeProjekt).Url;
    
        <img src="@image" style="width: 100%; height: 262px" />
    }
    

    I'm glad you've got a working solution now though! :)

Please Sign in or register to post replies

Write your reply to:

Draft