Been having a hard time cracking this, after trying every possible link on this forum & google results, including the official docs. Maybe I'm missing something...
All information is retrieved properly except for media Url for the article box, I'm getting the ID, printed both in data-background upon inspection as well as in the badge. Badge turns red when mediaId cannot be read or is not defined (in the case of demo course 2 it is not defined).
It appears that your property c.UlmsCourseImage contains only the id of the media item. In this respect, it isn't a media object - but rather a plain int.
In order to get the media URL, you'll need to use something like Umbraco.TypedMedia to retrieve the actual media item using the id. From there, you'll be able to access additional properties such as .Name and .Url
Something like the following should hopefully work:
So it seems that the solution indeed to try with a newly uploaded media file. Though it could retrieve the mediaId from the db, it didn't update for whatever reason.
The solution was to change c.UlmsCourseImage.Url to courseHasImage.Url plus var courseHasImage = c.UlmsCourseImage;
Media Id conversion to Url
Hey there!
Been having a hard time cracking this, after trying every possible link on this forum & google results, including the official docs. Maybe I'm missing something...
All information is retrieved properly except for media Url for the article box, I'm getting the ID, printed both in
data-background
upon inspection as well as in the badge. Badge turns red when mediaId cannot be read or is not defined (in the case of demo course 2 it is not defined).Running u7.14.
Here's the view
Here's a screenshot
Also here's the doctype setup
It appears that your property
c.UlmsCourseImage
contains only the id of the media item. In this respect, it isn't a media object - but rather a plain int.In order to get the media URL, you'll need to use something like
Umbraco.TypedMedia
to retrieve the actual media item using the id. From there, you'll be able to access additional properties such as.Name
and.Url
Something like the following should hopefully work:
Hi Rhys!
Thanks for your quick reply. Tried this and results into a server error
Am I lacking using or inherits statements?
It looks to me like you're getting a null exception, which might indicate that the
c.UlmsCourseImage
is empty.When you implemented the following:
Did you only apply this within the if statement?
Yes as it's the only property not being retrieved. I'm a total beginner so yeah, for sure some things are not very obvious to me :D
Changed the following:
to
and background url changred from
"1296"
to"background-image: url("/media/1004/mel-233281-unsplash.jpg");
yet still not a full path
&& updated media with freshly uploaded image, mediaId change to a corresponding one. So querying seems to be working fine.
Ah, that makes sense then - it looks like
c.UlmsCourseImage
is returning a media object afterall (my mistake!).In which case, you won't need to turn it into a media object, so you can ignore the
Umbraco.TypedMedia
stuff.Just to clarify then, does your image render properly now?
The
.Url
will give the relative path, so is there any particular reason why you need the absolute path?So it seems that the solution indeed to try with a newly uploaded media file. Though it could retrieve the mediaId from the db, it didn't update for whatever reason.
The solution was to change
c.UlmsCourseImage.Url
tocourseHasImage.Url
plusvar courseHasImage = c.UlmsCourseImage;
I'm glad everything is working now! #h5yr
Many thanks for your help! The solution came through thanks to your assistance 🙌
To others in need:
this code worked
is working on a reply...