Copied to clipboard

Flag this post as spam?

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


  • kodaxmax 5 posts 45 karma points
    Aug 31, 2023 @ 22:25
    kodaxmax
    0

    Media picker doesn't generate html

    I have a Image Media Picker and Multiple Image Media Picker in that order on a document type. In the back office im able to pick images and it appears to work fine.

    In browser the images are not displayed and instead the following to lines are:

    System.Collections.Generic.List`1[Umbraco.Cms.Core.Models.MediaWithCrops] 
    
    System.Linq.EmptyPartition`1[Umbraco.Cms.Core.Models.MediaWithCrops]
    

    Images within rich text fields do seem to display fine.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Sep 01, 2023 @ 07:19
    Huw Reddick
    0

    what does your html look like in your view where you are trying to display the image? You should be able to get the media/image like this:

    var image = Model.Value<MediaWithCrops>("image");
    if (image != null)
    {
        <img src="@image.MediaUrl()" alt="" />
    }
    

    If it's a multi picker you need to cast to IEnumerable

    var images = Model.Value<IEnumerable<MediaWithCrops>>("images")
    if (images != null && images.Any())
    {
        foreach (var image in images)
        {
            <img src="@image.MediaUrl()" alt="" />
        } 
    }
    

    Instead of MediaUrl() you can also use GetCropUrl() to return better compressed and optimized images.

  • Luuk Peters 85 posts 330 karma points
    Sep 01, 2023 @ 10:16
    Luuk Peters
    100

    The media picker does not generate HTML, it will provide data for you to make your own HTML. See @Huw Reddicks post for an example.

  • kodaxmax 5 posts 45 karma points
    Sep 01, 2023 @ 21:17
    kodaxmax
    0

    Thanks guys, i ahd indeed made a silly assumption that it was an automatic html generating method or partial view etc.. like other blocks.

    I kept getting errors witht "MediaWithCrops" type. I found "IPublishedContent" from another post that worked. I also found the images variable declaration only behaved in a @{} codeblock.

    my final code for future googlers:

    @{
        Layout = "Master.cshtml";
        var images = Model.Value<IEnumerable<IPublishedContent>>("gallery");
    }
    @Model.Value("title")
    @Model.Value("blurb")
    @Model.Value("fullDescription")
    
    @section gallery
    {
        @if (images != null && images.Any())
        {
            @foreach (var image in images)
            {
                    <img src="@image.GetCropUrl()" alt="" />
            }
        }
    

    }

Please Sign in or register to post replies

Write your reply to:

Draft