Copied to clipboard

Flag this post as spam?

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


  • Leo Henning 2 posts 72 karma points
    Nov 08, 2018 @ 22:25
    Leo Henning
    0

    How to display child image from query

    I am trying to iterate over a list of child content and show the name and the image.

    var dishwasherProducts = Model.Content.Site().FirstChild("dishLandingPage").Children()
                        .Where(x => x.GetPropertyValue<string>("category") == "Dish")
                        .Where(x => x.GetPropertyValue<string>("subCategory") == "dish - dishwasher")
                        .Where(x => x.IsVisible());
    

    But when I try to show the image I can't figure out how to do it.

            @foreach(var item in dishwasherProducts){
                <li>
                    <a href="@item.Url">
                        @item.GetPropertyValue("productName") 
                        <img src="@Umbraco.TypedMedia(item.GetPropertyValue("image")).Url"  alt="@item.GetPropertyValue("productName")"  />
                    </a>
                </li>
            }
    

    It works fine if I hard code a media ID like

         <img src="@Umbraco.TypedMedia(1141).Url" alt="@item.GetPropertyValue("productName")"  />
    

    And if I just output the image I get the media ID like

    @item.GetPropertyValue("image")
    
  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Nov 16, 2018 @ 13:56
    Dennis Adolfi
    0

    Hi Leo.

    Try the following:

    var media = item.GetPropertyValue<IPublishedContent>("image"); 
    <img src='@media.Url'>
    

    Best of luck to you!

  • Larry D. 1 post 71 karma points
    Nov 18, 2018 @ 09:04
    Larry D.
    0

    Hello Leo,

    I just finished that for my site, and here's the code that works for me:

    <img src="@Umbraco.Media(item.GetPropertyValue("articlePhoto").ToString()).Url" alt="@Umbraco.Field(item,"metaTitle")">
    

    So, you can replace some alias, and it will become:

    <img src="@Umbraco.Media(item.GetPropertyValue("image").ToString()).Url" alt="@Umbraco.Field(item,"productName")">
    

    I hope it will work for you too.

    Good luck!

Please Sign in or register to post replies

Write your reply to:

Draft