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
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" />
}
/ / 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" />
}
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:
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
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:
You mean something like this ?
i have solved it myself. i didt it with this ode here
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:
I'm glad you've got a working solution now though! :)
is working on a reply...