Copied to clipboard

Flag this post as spam?

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


  • Mohammad Javed 64 posts 373 karma points
    Nov 02, 2015 @ 14:36
    Mohammad Javed
    0

    Rendering Image inside foreach loop (Razor)

    Hello,

    I'm trying to render a image inside my foreach loop but having some trouble.

    My node structure is like this;

    • Section Node

      • Divider Node

        • Child Item Node

    Here is my code below;

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    <ul class="no-bullet mediaLister">
        @foreach (var child in Model.Content.Children.Where(c => c.DocumentTypeAlias.Equals("Blank")))
        {
            <li>
                <a href="@child.Url">@child.Name</a>
                @if (child.Children.Any(x => x.DocumentTypeAlias.Equals("MediaItem")))
                {
                    <ul class="no-bullet mediaItems">
                        @foreach (var page in child.Children.Where(p => p.DocumentTypeAlias.Equals("MediaItem")))
                        {
                            <li>
                                <a href="@page.Url">@page.Name</a>
    
                                <img class="img-thumbnail full" src="@Umbraco.TypedMedia(page.GetPropertyValue("mediaImage")).GetPropertyValue("umbracoFile")" alt="" />
    
                            </li>
                        }
                    </ul>
                }
            </li>
        }
    </ul>
    

    Can't quite remember how to render an image as part of two foreach loops - i've done it before so not sure where i am going wrong. :(


    UPDATE:

    I knew this would happen, as soon as i posted for help, BANG - It came in to my head on how to do it. I have to check there is a value if so render the image using Umbraco.TypedMedia & get property value.

    Updated code below;

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    <ul class="no-bullet mediaLister">
        @foreach (var child in Model.Content.Children.Where(c => c.DocumentTypeAlias.Equals("Blank")))
        {
            <li>
                <a href="@child.Url">@child.Name</a>
                @if (child.Children.Any(x => x.DocumentTypeAlias.Equals("MediaItem")))
                {
                    <ul class="no-bullet mediaItems">
                        @foreach (var page in child.Children.Where(p => p.DocumentTypeAlias.Equals("MediaItem")))
                        {
                            <li>
                                <a href="@page.Url">@page.Name</a>
    
                                @{
                                    if (page.HasValue("mediaImage"))
                                    {
                                        var mediaItem = Umbraco.TypedMedia(page.GetPropertyValue("mediaImage"));
                                        <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")" />
                                    }
                                }
    
                            </li>
                        }
                    </ul>
                }
            </li>
        }
    </ul>
    

    Thanks Javed

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Nov 02, 2015 @ 15:13
    Alex Skrypnyk
    0

    Hi Javed,

    Did you try yo get URL of media item like :

    <img src="@mediaItem.Url" alt="@mediaItem.GetPropertyValue("Name")" />
    

    Thanks

  • Mohammad Javed 64 posts 373 karma points
    Nov 02, 2015 @ 15:26
    Mohammad Javed
    100

    Hi Alex,

    I've amended the code above showing the working example.

    Thank you for replying though.

    Regards Javed

Please Sign in or register to post replies

Write your reply to:

Draft