Copied to clipboard

Flag this post as spam?

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


  • Hubert Thalmann 1 post 71 karma points
    Jun 26, 2018 @ 16:43
    Hubert Thalmann
    0

    Loop trough picked Media Folder

    Hello our.Umbraco Community

    I have a Media Picker where i Pick an Folder in my Media Section.

    How can i loop trough all images inside this media Folder?

    what i have is:

    var myFolder = Model.Content.GetPropertyValue("images")
    

    whats the next step?

    I want something like this (pseudocode)

    @foreach(var image in myFolder){<img src="image.Url"/>}
    

    please help

    Kind Regards Hubert

  • Frans de Jong 548 posts 1840 karma points MVP 4x c-trib
    Jun 26, 2018 @ 19:54
    Frans de Jong
    0

    Hi Hubert,

    This is a partial view I use for a doctype grid editor I use in one of the older projects.

    There is some stuff you don't need for instance a title and all the markup but it should at least get you started. If you have any questions please do ask.

    @if (Model != null && Model.MediaFolder != null)
    {
        if (Model.Title != null)
        {
            <h2>@Model.Title</h2>
        }
        if (Model.MediaFolder.DocumentTypeAlias != "Folder")
        {
            <a href="@Model.MediaFolder.Url">@Model.MediaFolder.Name</a>
        }
        else
        {
            <ul class="DownloadTree tree">
                @foreach (IPublishedContent node in Model.MediaFolder.Children)
                {
                    if (node.DocumentTypeAlias == "Folder")
                    {
                        @RenderFolder(node, false)
                    }
                    else
                    {
                        @RenderFile(node, false)
                    }
                }
            </ul>
        }
    }
    
    
    @helper RenderChildren(IEnumerable<IPublishedContent> nodes)
        {
            <ul>
                @foreach (var node in nodes)
                {
                    if (node.DocumentTypeAlias == "Folder")
                    {
                        @RenderFolder(node, true)
                    }
                    else
                    {
                        @RenderFile(node, true)
                    }
                }
            </ul>
    }
    
    @helper RenderFolder(IPublishedContent node, bool hide)
        {
            int childrenCount = node.Children.Count();
            <li class="treeFolder @(childrenCount != 0 ? "branch":"")" style="@(hide ? "display: none;": "display: list-item;")">
                <i class="indicator fa fa-folder"></i>
                <a href="#" class="@(childrenCount == 0 ? "empty":"")">@node.Name (@childrenCount)</a>
                @if (childrenCount > 0)
                {
                    @RenderChildren(node.Children)
                }
            </li>
    }
    
    @helper RenderFile(IPublishedContent node, bool hide)
        {
            <li class="treeDownload" style="@(hide ? "display: none;": "display: list-item;")">
                @{
                    <a href="@node.Url" target="_blank" download>@node.Name</a>
                }
            </li>
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft