In my Umbraco 7.15.7 I'm displaying images in the Partial Views Macro Files as follow:
figure>
@if (newsItem.HasValue("articleImg"))
{
<img src="@Umbraco.Media(newsItem.articleImg.ToString()).Url" class="img-fluid news-img" alt="@newsItem.Last().photoAlt" />
}
(the reason for the .ToString() is after I switched from Obsolete to Media.Picker2 - so since it's string)
In my Umbraco 11.1 I was struggle to display the images. I tried the following and it didn't display the images, but also didn't throw an error, just didn't display the images:
<img src="@Umbraco.Media(newsItem.Value<IPublishedContent>("articleImg").ToString()).Url()" class="img-fluid" alt="@Imaging_pagesToList.Last().Value("photoAlt")" />
So I tried this one:
<img src="newsItem.Value<IPublishedContent>("articleImg").Url()" class="img-fluid" alt="@newsItem.Value("photoAlt")" />
And this one also doesn't show any image
I also tried this one (the same as above, just with @ - @newsItem) , and this throw an error: "Argument 3: cannot convert from 'method group' to 'object?'":
<img src="@newsItem.Value<IPublishedContent>("articleImg").Url()" class="img-fluid" alt="@newsItem.Value("photoAlt")" />
So I tried this one:
var image = newsItem.Value<IPublishedContent>("articleImg");
<img src="@image.Url()" class="img-fluid" alt="@newsItem.Value("photoAlt")" />
And this is finally works! And I don't understand why? (why this works, and why the previous didn't work)
My question are:
Why only the last one works?
Why the first two doesn't work?
Is this is the right way to render images in Umbraco 11.1 ?
In the official documentations of Umbraco (9 and up) they suggest to render images as follow:
var mediaItem = Umbraco.Media(Guid.Parse("55240594-b265-4fc2-b1c1-feffc5cf9571"));
But they hard coding the Guid in their examples. Not sure how to get the Guid for each node.
Please advise.
Thanks.