Copied to clipboard

Flag this post as spam?

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


  • Simeon Ostberg 123 posts 389 karma points
    Oct 21, 2020 @ 12:16
    Simeon Ostberg
    0

    Define Image cropper in partial view

    Hello everyone,

    I would like to implement the image cropper, however I get stuck in the definition area. This is my code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web
    @{
        var UmbHelper = new UmbracoHelper(UmbracoContext.Current);
        var thisPageId = 12855;  
        var stringPage = @ViewData["pageId"];  
        thisPageId = Convert.ToInt32(stringPage); 
    
        var pageinfo = UmbHelper.Content(thisPageId);
        var cropUrl = Url.GetCropUrl(pageinfo.image, "pagecontent_stz", false);
    }
    

    This gives me this error: CS1973: 'UrlHelper' has no applicable method named 'GetCropUrl' but appears to have an extension method by that name.

    Does anyone have an idea?

    Thanks a lot! Simeon

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Oct 22, 2020 @ 06:22
    Marc Goodson
    0

    Hi Simeon

    The extensions method you are looking to use has the signature:

     public static IHtmlString GetCropUrl(this UrlHelper urlHelper, IPublishedContent mediaItem, string cropAlias, bool htmlEncode = true)
    

    So I 'think' - that the issue here might be how you are passing in the IPublishedContent item into the helper - because you are using V7, Umbraco.Content() doesn't return an IPublishedContent item it returns a dynamic object - if you use Umbraco.TypedContent() you should get the object back as an IPublishedContent item:

        @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
        @using Umbraco.Web
        @{
        //because you are inheriting from UmbracoTemplatePage you already have access to the UmbracoHelper via @Umbraco
           // var UmbHelper = new UmbracoHelper(UmbracoContext.Current);
        //think this is just whilst you are testing? - Model.Content.Id should give you the current page id    
    
            var thisPageId = 12855;  
            var stringPage = @ViewData["pageId"];  
            thisPageId = Convert.ToInt32(stringPage); 
    
            var pageinfo = Umbraco.TypedContent(thisPageId);
            //pageInfo is now an IPublishedContent item representing the page iwth Id 12855 - if this is the 'current' page you could access this via Model.Content
    
            // now get the IPublishedContent item representing the Media Item (this uses PropertyValueConverters to convert the picked image into IPublishedContent 
    
        var mediaItem = pageInfo.HasValue("image") ? pageInfo.GetPropertyValue<IPublishedContent>("image") : default(IPublishedContent);
    
            //if you don't have PropertyValueConverters enabled < v7.6 then 
            // var mediaItem = pageInfo.HasValue("image") ? Umbraco.TypedMedia(pageInfo.GetPropertyValue("image")) : default(IPublishedContent);
    
            if (mediaItem!=null){
                 var cropUrl = Url.GetCropUrl(mediaItem, "pagecontent_stz", false);
    }
    

    }

    Anyway hopefully that gives the gist of what it might not be working!

    regards

    Marc

  • Simeon Ostberg 123 posts 389 karma points
    Oct 22, 2020 @ 08:14
    Simeon Ostberg
    0

    Hi Marc,

    Thank you for your reply! When using this code

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web
    @{
        var thisPageId = 12855;  
        var stringPage = @ViewData["pageId"];  
        thisPageId = Convert.ToInt32(stringPage); 
    
        var pageinfo = Umbraco.TypedContent(thisPageId);
        var mediaItem = pageInfo.HasValue("image") ? pageInfo.GetPropertyValue<IPublishedContent>("image") : default(IPublishedContent);
        if (mediaItem!=null){
            var cropUrl = Url.GetCropUrl(mediaItem, "pagecontent_stz", false);
        }
    }
    

    I get, that there is a "}" missing, what I don't understand.

    Thanks! Simeon

  • Simeon Ostberg 123 posts 389 karma points
    Oct 27, 2020 @ 14:29
    Simeon Ostberg
    0

    Hi everyone,

    I would be happy to get any idea...

    Thanks a million!

    Best, Simeon

  • Simeon Ostberg 123 posts 389 karma points
    Nov 02, 2020 @ 12:02
    Simeon Ostberg
    100

    Hi everyone,

    I finally found the solution with the help of this thread: https://our.umbraco.com/forum/templates-partial-views-and-macros/87310-763-image-cropper-again

    This is the code, which finally worked:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web
    @using Umbraco.Web.Models
    @{
        var UmbHelper = new UmbracoHelper(UmbracoContext.Current);
        var thisPageId = 12855; 
        var stringPage = @ViewData["pageId"]; 
        thisPageId = Convert.ToInt32(stringPage);
    
        var pageinfo = UmbHelper.Content(thisPageId);
        var contentImage = pageinfo.GetPropertyValue("image");
    
    }
    
    <div class="image-container">
      @if (contentImage != null) {
        int imageID = @contentImage.Id;
        var imageItem = Umbraco.TypedMedia(@imageID);   
        <img class="img-fluid lazy" data-original="@Url.GetCropUrl(imageItem, "pagecontent_stz")" src="@Url.GetCropUrl(imageItem, "pagecontent_stz")" alt="" />
      }
    </div>
    

    Best, Simeon

Please Sign in or register to post replies

Write your reply to:

Draft