Copied to clipboard

Flag this post as spam?

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


  • Steve Wilkinson 132 posts 211 karma points
    Dec 14, 2021 @ 12:23
    Steve Wilkinson
    0

    Accessing media files in a partial view

    I want to write in a partial view, a dynamic carousel that pulls images from a selected folder.

    I'm struggling to find how you do this in v9 and the sort of code I'd have used in v7 obviously doesn't work with all the changes.

    Has anyone got a simple example of how you do this in v9 please?

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Dec 14, 2021 @ 15:54
    Marc Goodson
    100

    Hi Steve

    If you create a 'Media Picker' on a document type that is only allowed to pick one Folder, and give it alias 'carouselFolder'

    Then the following in a partial view would read the folder, and loop through it's children:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage
    @using Umbraco.Cms.Core
    @using Umbraco.Cms.Core.Models.PublishedContent
    @using Umbraco.Cms.Core.Routing
    @using Umbraco.Extensions
    
    @inject IPublishedUrlProvider PublishedUrlProvider
    
    @{ 
    
    var mediaFolder = Model.Value<IPublishedContent>("carouselFolder");
    var selection = mediaFolder.Children();
    }
    
    @if (selection!=null && selection.Any())
    {
        <ul>
            @foreach (var item in selection)
            {
                <li>
                    <a href="@item.Url(PublishedUrlProvider)">
                    <img src="@Url.GetCropUrl(item,height: 50, width: 50)" />
                    <br />@item.Name</a>
                </li>
            }
        </ul>
    }
    

    Broadly speaking this is the same as V8... or you may be using ModelsBuilder in which case

    mediaFolder = Model.CarouselFolder;
    

    would I think also work

    so GetCropUrl is still the way to get a crop of an image that you have retrieved the IPublishedContent representation of.

    the only difference is injecting the PublishedUrlProvider into the Url() method... but I think that has been updated to work without it too...

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft