Copied to clipboard

Flag this post as spam?

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


  • Jessie Yeagle 3 posts 73 karma points
    May 03, 2024 @ 13:37
    Jessie Yeagle
    0

    Trouble accessing media picker image within document type in template

    I have created a document type with no template named "Application" and have added an Image Media Picker to it with an alias of "logo" and a textstring with an alias of "description".

    I have created a couple instances of that document type and have attached them to another document type named "Home" as children.

    In Home's template I am attempting to show the selected images by doing the following:

    @using Umbraco.Cms.Web.Common.PublishedModels;
    @using Umbraco.Cms.Core.Models;
    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @{
        Layout = "Master.cshtml";
    }
    
    
    @{
        IEnumerable<IPublishedContent> featureBlocks = Model.ChildrenOfType("Application");
    }
    
    @foreach(var block in featureBlocks) {
    
        @block.Value("Logo")
        <br>
        @block.Value("description")
    }
    

    The descriptions come through just fine.

    The logo outputs:

    Umbraco.Cms.Core.Models.MediaWithCrops`1[Umbraco.Cms.Web.Common.PublishedModels.Image]

    I am trying to access the media image so I can display it on the page.

    I searched online and saw suggestions like:

    @block.Value<MediaValueWithCrops>("Logo").MediaUrl()
    //or
    @block.Value<MediaValueWithCrops>("Logo").Url()
    

    I have tried this adding the generic type specifier by doing @block.Value. When I do that my template fails to load and I get the following exception on my logs.

    Umbraco.Cms.Web.Common.ModelsBuilder.InMemoryAuto.UmbracoCompilationException: Exception of type 'Umbraco.Cms.Web.Common.ModelsBuilder.InMemoryAuto.UmbracoCompilationException' was thrown. at Umbraco.Cms.Web.Common.ModelsBuilder.InMemoryAuto.CollectibleRuntimeViewCompiler.CompileAndEmit(String relativePath) at Umbraco.Cms.Web.Common.ModelsBuilder.InMemoryAuto.CollectibleRuntimeViewCompiler.OnCacheMiss(String normalizedPath)

    If I try do access a property I also get an error:

    @block.Value("Logo").Content
    

    yields this in the logs:

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

    Does anyone have any suggestions about what is going wrong? I did not think it would be this tough to get the image out of the media picker.

    Here are the associate docs: https://docs.umbraco.com/umbraco-cms/fundamentals/backoffice/property-editors/built-in-umbraco-property-editors/media-picker-3

  • Huw Reddick 1875 posts 6421 karma points MVP 2x c-trib
    May 06, 2024 @ 09:24
    Huw Reddick
    0

    Hi Jessie,

    try using the below

    @block.Value<MediaWithCrops>("logo").Url() and this needs to be used in the src attribute of an img tag to display the image

  • Jessie Yeagle 3 posts 73 karma points
    May 06, 2024 @ 19:40
    Jessie Yeagle
    0

    I continued to receive an error when trying to specify MediaWithCrops as the generic.

    Here is what I had to do to get the logo to render:

    @using Umbraco.Cms.Core.Models;
    
    //further down
    
    @{
        IEnumerable<IPublishedContent> apps = Model.ChildrenOfType("Application");
    }
    
    
    
    <ul>
    
    @foreach(var app in apps ) {
        var logo = app.Value("Logo") as MediaWithCrops;
        <li>
            <img src="@logo.Url()" />
       </li>
    }
    </ul>
    
Please Sign in or register to post replies

Write your reply to:

Draft