Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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" />
Please help :-)
What version of Umbraco are you using?
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.
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.
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
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.
Is there a missing ) in that Code you sent?
Ahh, yes sorry. Should be a double )) at the end of Insurance Tips" instead of the single that is currently there.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted