Copied to clipboard

Flag this post as spam?

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


  • Rob Clayton 3 posts 83 karma points
    Apr 29, 2020 @ 12:31
    Rob Clayton
    0

    Umbraco 8 render image from IPublishedContent

    Hey

    I am using Umbraco 8 for the first time and being tripped up a lot vs Umbraco 6/7.

    So I am trying to render some images in the _Layout partial view. From a list of items in the root. Document type below.

    enter image description here

    I used the Query builder in the _Layout.cshtml to end up with the following to iterate over the logos.

    @{
    var selection = Umbraco.Content(Guid.Parse("92c903b5-4772-48d5-8e16-5238e45c1e36"))
    .ChildrenOfType("footerLogo")
    .Where(x => x.IsVisible());
    }
    <ul>
        @foreach (var item in selection)
        {
        <li>
            <a href="@item.Url">@item.Name</a>
    
    
    // I added this code and put a breakpoint in VS2019 to inspect the object
           @item
    // Using the immediate window, this gave me a full object
           @item.GetProperty("Logo").GetValue()
    

    enter image description here

    I've tried to access the Url property with

     item.GetProperty("Logo").GetValue().Url
    

    but getting exception

     cannot convert from 'method group' to 'HelperResult'
    

    I've spent a couple of hours on googling Umbraco 8 image rendering but no luck. Just wondering if someone could give me a pointer on how to get at these values?

    Many Thanks

    Rob

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 29, 2020 @ 12:36
    Alex Skrypnyk
    100

    Hi Rob,

    Try this:

    var imageUrl = item.Value<IPublishedContent>("Logo").Url;
    

    Thanks,

    Alex

  • Rob Clayton 3 posts 83 karma points
    Apr 29, 2020 @ 12:41
    Rob Clayton
    0

    Hi Alex,

    Worked perfectly! So casting the object type gives you access to the props? Good to know!

    Thanks a lot dude

    Rob

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 29, 2020 @ 19:43
    Alex Skrypnyk
    0

    Hi Rob,

    It's always nice to use strongly typed objects, luckily Umbraco has this feature so use it

    Thanks,

    Alex

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Apr 29, 2020 @ 13:50
    Alex Skrypnyk
    0

    Yes, You are always welcome.

Please Sign in or register to post replies

Write your reply to:

Draft