Copied to clipboard

Flag this post as spam?

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


  • Barry Corrigan 32 posts 145 karma points
    Jan 14, 2019 @ 10:56
    Barry Corrigan
    0

    Images not displaying in my foreach loop

    I have this for each loop which is outputting content fine, it's just my images are not displaying has anyone any ideas why?

    <ul class="tipsGuideList row">
        @foreach (var p in CurrentPage.AncestorOrSelf(1).Children.First("Name == \"Area - Expert Travel Insurance Tips\"").Children.Random(3))
        {
            <li class="tipsGuideList__item col-xs-12 col-md-4">
                <a href="@p.Url" class="tipsGuideList__link">
                    @{
                        var dynamicMediaItem = Umbraco.Media(p.GetPropertyValue<int>("tipIcon"));
                        <img src="@dynamicMediaItem.Url" alt="@p.Name" width="140" />
                    }
                    <span class="tipsGuideList__text">@p.Name</span>
                </a>
            </li>
        }
    
    </ul>
    

    Please help :-)

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jan 14, 2019 @ 11:12
    Nik
    0

    Hi Barry,

    What version of Umbraco are you using?

    Thanks

    Nik

  • Barry Corrigan 32 posts 145 karma points
    Jan 14, 2019 @ 11:19
    Barry Corrigan
    1

    Hi Nik,

    Its Version 7.12.3. But I've just realised ive made a massive error, when debugging this the image was coming up as null. Went and check the document type and that particular image field wasn't included :-( :-)

    Thanks for the help and making me go into Umbraco and check the version, which lead me to check the Doc Type Properties.

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jan 14, 2019 @ 11:26
    Nik
    0

    No problem,

    Just a couple of bits of feedback, it would best no to use Dynamics if you can avoid it.

    So instead of CurrentPage.AncestorOrSelf(1) you could do Model.Content.AncestorOrSelf(1) to get a typed model instead of dynamic.

    In addition, use Umbraco.TypedMedia instead as well.

    Another thing, 7.12.3 come with Property Value Converters which should (I think) be enabled by default.

    So if you go down the typed model route you should be able to do this:

    @{
        var mediaItem = p.GetPropertyValue<IPublishedContent>("tipIcon");
    }
    

    This should in theory get your the media item (assuming one is present still).

    When accessing media or content, it's worth adding in a null check as well just to make sure content is still valid.

    Hope you don't mind the feedback.

    Nik

  • Barry Corrigan 32 posts 145 karma points
    Jan 14, 2019 @ 11:38
    Barry Corrigan
    0

    Thanks for the help.

    I tried Model.Content previously but it just gave me this error

     'System.Collections.Generic.IEnumerable<Umbraco.Core.Models.IPublishedContent>' does not contain a definition for 'First' and the best extension method overload 'System.Linq.Enumerable.First<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' has some invalid arguments
    
  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jan 14, 2019 @ 11:44
    Nik
    0

    Ahh,

    Yes that would be because the First function would be wrong in the typed approach.

    This is what it would need to change to:

    Model.Content.AncestorOrSelf(1).Children.First( p => p.Name.Equals("Area - Expert Travel Insurance Tips").Children.Random(3)
    

    The First expression you'd typed was targeting dynamics so you get the error you saw. This turns the same thing into a typed query in Linq.

    Give that a go and see how you get on.

    Nik

  • Barry Corrigan 32 posts 145 karma points
    Jan 14, 2019 @ 11:52
    Barry Corrigan
    0

    Is there a missing ) in that Code you sent?

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jan 14, 2019 @ 11:54
    Nik
    0

    Ahh, yes sorry. Should be a double )) at the end of Insurance Tips" instead of the single that is currently there.

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft