Copied to clipboard

Flag this post as spam?

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


  • Craig100 1136 posts 2523 karma points c-trib
    Mar 15, 2017 @ 14:43
    Craig100
    0

    Crop Url from Nested Media Picker

    Umb 7.5.11 with Nested Content and Umbraco Property Value Converters installed.

    Really stuck trying to get the img url out of this partial. The nested doc type is just a textstring (textOverlay) and a multimediapicker set to pick a single image (slideImage). It all works apart from the img src which is this: @Umbraco.TypedMedia(sliderItem.GetPropertyValue("slideImage")).GetCropUrl("Slider") If I remove .GetCropUrl("Slider") the page displays with the slider working but no image (textOverlay is displayed correctly). If I leave it in then I get a YSOD saying: "System.ArgumentNullException: Value cannot be null. Parameter name: mediaItem"

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedContent>
    
    <div class="flexslider">
        <ul class="slides">
            @{
    
            if(Model.HasValue("sliderImages")) {
    
                var slides = Model.GetPropertyValue<IEnumerable<IPublishedContent>>("sliderImages");
                foreach(IPublishedContent sliderItem in slides) {
                    <li>
                        <img src="@Umbraco.TypedMedia(sliderItem.GetPropertyValue("slideImage")).GetCropUrl("Slider")" alt="@sliderItem.GetPropertyValue("TextOverlay")">
                        <div class="sliderCaption">@sliderItem.GetPropertyValue("TextOverlay")</div>
                    </li>
                    }
                }
            }
        </ul>
    </div>
    

    If I output: sliderItem.GetPropertyValue("slideImage") then I just get a string: "Umbraco.Web.PublishedContentModels.Image" which I understand is from the ModelsBuilder. Stuck.

    Any advice would be appreciated.

    -Craig

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Mar 15, 2017 @ 15:58
    Kevin Jump
    0

    Hi

    I think with models builder in play you don't need to do the Umbraco.TypedMedia call.

    it looks like your call to sliderItem.GetPropertyValue("slideImage") is returning an image model.

    so you should be able to get the crop direct:

    sliderItem.GetPropertyValue("slideImage").GetCropUrl("Slider")
    

    personally i would seperate it out a little but the above should work if the values are there.

    var img = sliderItem.GetPropertyValue<Image>("slideImage");
    if (img != null) 
    {
           <img src="img.GetCropUrl("Slider")"> ...
    }
    
  • Craig100 1136 posts 2523 karma points c-trib
    Mar 15, 2017 @ 16:08
    Craig100
    0

    Thanks Kevin,

    Doesn't appear to work though:-

    with @img.GetCropUrl("Slider")........

    'object' does not contain a definition for 'GetCropUrl' and the best extension method overload 'Umbraco.Web.ImageCropperTemplateExtensions.GetCropUrl(Umbraco.Core.Models.IPublishedContent, string)' has some invalid arguments
    

    So how to break into the object to get the goodies?

  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Mar 15, 2017 @ 16:17
    Kevin Jump
    100

    :(

    I have code that gets image and GetCropUrl works -

    Image inherits from IPublishedContent so it should let you do that.

    if you are not using models builder, you can turn it off in the web.config (set enabled to false).

    if you have Core Value Property Converters installed:

    change :

    var img = sliderItem.GetPropertyValue<Image>("slideImage");
    

    to

    var img = sliderItem.GetPropertyValue<IPublishedContent>("slideImage");
    

    if not -with model builder off - back to typed content should work.

    var imgId = sliderItem.GetPropertyValue<int>("slideImage");
    var img = Umbraco.TypedContent(imgId) 
    

    if you want to use models builder make sure you have core value converters installed - seems to work much nicer when that is in

  • Craig100 1136 posts 2523 karma points c-trib
    Mar 15, 2017 @ 16:28
    Craig100
    0

    Doing var img = sliderItem.GetPropertyValue<IPublishedContent>("slideImage"); did the trick. I should have thought of trying that cast that to be honest.

    Many thanks :)

Please Sign in or register to post replies

Write your reply to:

Draft