Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
If you, in a Media Picker, pick an image and then deletes the image in the media section, the image id is still stored in the picker.
How do I check for this scenario in Razor ?
If I don't do anything the code will fail.
In this case it would be best to null check the image before using it in your code e.g.
@{ var imageId = Model.Content.GetPropertyValue<int>("mediaPickerAlias"); var image = Umbraco.TypedMedia(imageId); } @if(image !=null){ <img src="@image.Url"/> }
or in newer c# language versions, just this
@{ var imageId = Model.Content.GetPropertyValue<int>("mediaPickerAlias"); var image = Umbraco.TypedMedia(imageId); } <img src="@image?.Url"/>
Umbraco v8...
var image = Model.Value<IPublishedContent>("mainImage"); var imageId = image != null ? image.Id : 0; var image = imageId =! 0 ? Umbraco.Media(imageId): null;
//Or...
var siteRoot = Model.Root(); var image = Model.Value<IPublishedContent>("mainImage"); var imageId = image != null ? image.Id : 0; var image = imageId =! 0 ? Umbraco.Media(imageId) : siteRoot.Value<IPublishedContent>("defaultImage") ;
//Or maybe with Fallback...
var siteRoot = Model.Root(); var image = Model.Value<IPublishedContent>("mainImage", fallback: Fallback.ToAncestors); var imageId = image != null ? image.Id : 0; var image = imageId =! 0 ? Umbraco.Media(imageId) : siteRoot.Value<IPublishedContent>("defaultImage") ;
Hope this works for you and saves the countless hours spent pulling hair.
Best, Daniel
I created a new post for this in case anyone else was having the same issue. Nik was very helpful with more information on this issue.
Here is the thread...
https://our.umbraco.com/forum/using-umbraco-and-getting-started//101391-v8-handling-trashed-images-that-are-being-used-in-the-content-section
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
How to check for deleted images ?
If you, in a Media Picker, pick an image and then deletes the image in the media section, the image id is still stored in the picker.
How do I check for this scenario in Razor ?
If I don't do anything the code will fail.
In this case it would be best to null check the image before using it in your code e.g.
or in newer c# language versions, just this
Umbraco v8...
//Or...
//Or maybe with Fallback...
Hope this works for you and saves the countless hours spent pulling hair.
Best, Daniel
I created a new post for this in case anyone else was having the same issue. Nik was very helpful with more information on this issue.
Here is the thread...
https://our.umbraco.com/forum/using-umbraco-and-getting-started//101391-v8-handling-trashed-images-that-are-being-used-in-the-content-section
is working on a reply...