Copied to clipboard

Flag this post as spam?

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


  • Sebastian Dammark 581 posts 1385 karma points
    Nov 12, 2018 @ 19:03
    Sebastian Dammark
    0

    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.

  • louisjrdev 107 posts 344 karma points c-trib
    Nov 13, 2018 @ 15:01
    louisjrdev
    1

    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"/>
    
  • Daniel Gillett 72 posts 149 karma points
    Mar 04, 2020 @ 09:51
    Daniel Gillett
    0

    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

  • Daniel Gillett 72 posts 149 karma points
    Mar 04, 2020 @ 10:46
    Daniel Gillett
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft