You may get this error if mediaId is empty.
Make sure that mediaId is not null or empty.
Have you try like this one.
var mediaId = Umbraco.Field("heroImage");
if (!string.IsNullOrEmpty(mediaId.ToString()))
{
var media = Umbraco.Media(mediaId);
if (media != null)
{
<img src="@media.Url">
}
}
Look at the value of mediaId first, then also check that the field heroImage exists. If mediaId is null then that would explain your issue.
Personally, I would use CurrentPage instead of Umbraco to access the property, I'd also wrap the code in a check to ensure the property exists in the first place.
Not sure why, but It might be that Umbraco.Field returns IHtmlString type so parsing it to string or integer, it always uses strong type instead of object.
Spot on Jvan, it does indeed look like Umbraco.Field returns IHtmlString - it was working in my macro because i was using MacroParameters not Umbraco.Field, had to come away from the screen for 5 before i realised!
In this documentation you will get the Razor snippet for how to get data from the different property editors. In both version the dynamic version and the strongly typed version.
Hi there,
I have had the same problem. Running in VS2015 debugger, I noticed that mediaId has indeed the "Id" property Id (capital "I"), which has an int value. The exception on the other hand is talking about the "id" property (small "i"). Could this difference explain the reason why the exception is being thrown in the first place?
Greetz,
Andre
Getting media url using ID from property needs Integer or string
Hi,
I'm using the following in a Macro and it works great however when i use it in my Master template, or any other template i get an error.
But it is an Integer and works fine in macros and mediaId is giving me the media Id!- i must be missing something really obvious!
TIA
You may get this error if mediaId is empty. Make sure that mediaId is not null or empty.
Have you try like this one.
Thanks for the reply Jivan, the mediaId is not null
If i do:
I get
If i do this it works:
So confused!
Hum
what if you parse IHtmlString to string and get the media.
Bingo - thanks very much for your help.
Please can you explain why that was needed in Template but not in Macro?
Hey Danny,
Have you tried checking what your line:
results in?
Look at the value of mediaId first, then also check that the field heroImage exists. If mediaId is null then that would explain your issue.
Personally, I would use CurrentPage instead of Umbraco to access the property, I'd also wrap the code in a check to ensure the property exists in the first place.
Thanks Nik, as above mediaId = 1125
That's why i don't understand why i needed toString when an int should work too!
I'll be wrapping it in a conditional for sure - will explore CurrentPage aswell.
Not sure why, but It might be that Umbraco.Field returns IHtmlString type so parsing it to string or integer, it always uses strong type instead of object.
Spot on Jvan, it does indeed look like Umbraco.Field returns IHtmlString - it was working in my macro because i was using MacroParameters not Umbraco.Field, had to come away from the screen for 5 before i realised!
Thanks again for the help.
Hi Danny,
Try to see this overview https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/ of the different property editors, that you find in Umbraco.
In this documentation you will get the Razor snippet for how to get data from the different property editors. In both version the dynamic version and the strongly typed version.
Hope this helps,
/Dennis
Perfect, many thanks Dennis.
Hi there, I have had the same problem. Running in VS2015 debugger, I noticed that mediaId has indeed the "Id" property Id (capital "I"), which has an int value. The exception on the other hand is talking about the "id" property (small "i"). Could this difference explain the reason why the exception is being thrown in the first place? Greetz, Andre
is working on a reply...