Copied to clipboard

Flag this post as spam?

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


  • sildee 2 posts 41 karma points
    May 13, 2024 @ 12:55
    sildee
    0

    Accessing image cropper in Master Page

    Hi all, I'm having some issues with a problem that sounds like it should be relatively simple. All my templates have an optional Image Cropper property with which the editor can select an image to use as a banner when desired. Currently this is implemented on one of my templates in the following way:

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<ContentModels.Vacature>
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @using Umbraco.Extensions;
    @{
        Layout = "MasterPage.cshtml";
         if(Model.BannerFoto is not null){
            var banner = Url.GetCropUrl(Model.BannerFoto,"Banner");
            <section>
                <div class="jumbotron jumbotron-fluid jumbotron-fluid-empty-banner mt-0 mb-0" style="background-image:url(@banner)">
                </div>
            </section>
        } else {
        } 
    }
    

    Copying this same code to the master page results in the following error:

    'IPublishedContent' does not contain a definition for 'BannerFoto' and no accessible extension method 'BannerFoto' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    What's causing this? I am using the same usings in both pages and I'm not sure what I'm doing wrong. Here are the usings in the master page:

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @using ContentModels = Umbraco.Cms.Web.Common.PublishedModels;
    @using Umbraco.Extensions;
    @{
        Layout = null;
    }
    

    Any help would be much appreciated.

  • Danine Noble 82 posts 368 karma points
    May 13, 2024 @ 19:02
    Danine Noble
    0

    Hullo Sildee ^^

    This is caused by the Master being a generalized, not-strongly-typed Model versus your page templates which are strongly-typed.

    Strongly-typed models give access to template fields assigned to the template like so: Model.FieldName.

    But the master template is just a general IPublishedContent model. It does not know what fields are assigned to any other model type. You can still access them but it must be done more generically.

    Such as, for your example:

    Model.Value<MediaWithCrops>("bannerFoto")
    

    The official documentation has examples for all property editors using strongly typed models and not.

  • sildee 2 posts 41 karma points
    May 24, 2024 @ 09:27
    sildee
    101

    Thank you Danine,

    I ended up fixing it quite simply this way:

    var banner = Model.GetCropUrl("bannerFoto", "Banner");  
    
Please Sign in or register to post replies

Write your reply to:

Draft