Copied to clipboard

Flag this post as spam?

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


  • Dejan Stojanovic 12 posts 51 karma points
    May 15, 2019 @ 16:50
    Dejan Stojanovic
    0

    Reading Custom Media Image property throws System.ArgumentException

    I created custom Image in media which has some addition crop settings. I called this media type ArticleImage.

    Now when I try to read it in a partial view I am getting System.ArgumentException with the following message

    The value "Umbraco.Web.PublishedModels.ArticleImage" is not of type "Umbraco.Web.PublishedModels.Image" and cannot be used in this generic collection. Parameter name: value

    I tried using

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<dynamic> 
    

    instead of default

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    

    but I am still getting the same exception, like there is something wrong with Umbraco8 and custom media image type.

    Any idea how to resolve this?

  • Claushingebjerg 939 posts 2574 karma points
    May 16, 2019 @ 12:23
    Claushingebjerg
    0

    what does your media type look like, and how are you trying to output it?

  • Dejan Stojanovic 12 posts 51 karma points
    May 16, 2019 @ 16:02
    Dejan Stojanovic
    0

    Hi,

    Here is how the media type and the data type look like Custom Image Media type

    and the data type used for umbracoFile property enter image description here

    I am accessing it with an extension method I wrote

      public static String GetImageCropUrl(this IPublishedContent content, String propertyAlias, String cropAlias)
            {
                if (content.HasProperty(propertyAlias))
                {
                    var umbHelper = Umbraco.Web.Composing.Current.UmbracoHelper;
                    var propValue = content.GetProperty(propertyAlias).GetSourceValue();
                    var media = umbHelper.Media(propValue);
                    return media?.GetCropUrl(cropAlias);
                }
                return null;
            }
    
            public static String GetImageUrl(this IPublishedContent content, String propertyAlias)
            {
                if (content.HasProperty(propertyAlias))
                {
                    var umbHelper = Umbraco.Web.Composing.Current.UmbracoHelper;
                    var propValue = content.GetProperty(propertyAlias).GetSourceValue();
                    return umbHelper.Media(propValue)?.Url;
                }
                return null;
            }
    

    I am getting exception in views where I am not eve trying to get the image crop path with extension method. For example I have document type Article which has a property of MediaPicker which is set to only pick ArtcleImage media and when I loop through children of parent of Artcle document I am getting the exception I mentioned above

Please Sign in or register to post replies

Write your reply to:

Draft