In Umbraco I created a homepage and a contact page. I created a media picker for the user to choose a picture to be displayed on the homepage. But I want to display this user selected image on the contact page as well (to keep the user from having to select the same image twice).
How can I build this in Umbraco using Razor?
Here's my code to display the image on the homepage:
@{ var myNode = @Model.NodeById(1049); <img src="@Model.Media("actie_foto", "umbracoFile")" id="actie_foto"/> }
As you can see, the homepage ID is 1049, and the image by the user selected in the field 'actie_foto' is loaded perfectly. But when I want to reuse this code on the contact page, no image is shown. I mean, I just see this:
<img src="" id="actie_foto"/>
An empty img src on the contact page while I'm using the exact same code for the contact page, it's just on an other page!
It looks like you are getting "myNode", but then not using it. Here's maybe another way to try this:
@{ var myNode = @Model.NodeById(1049); var myMedia = Library.MediaById(myNode.actie_foto); <img src="@myMedia.umbracoFile" id="actie_foto"/> }
And there is actually another way to do this using "recursive" properties, which basically means, if the property can't be found on the current node, then keep stepping up the ancestor chain until a node is found that does have a value. I actually prefer this approach for things like a default image, which the site owner can maybe change for just a certain section in one place, but not needed to be set on every single page. That would look like this:
@{ var myMedia = Library.MediaById(myNode._actie_foto); // Notice leading underscore on property, meaning recursive <img src="@myMedia.umbracoFile" id="actie_foto"/> }
Re-use fields
I have a pretty simple question:
In Umbraco I created a homepage and a contact page.
I created a media picker for the user to choose a picture to be displayed on the homepage.
But I want to display this user selected image on the contact page as well
(to keep the user from having to select the same image twice).
How can I build this in Umbraco using Razor?
Here's my code to display the image on the homepage:
As you can see, the homepage ID is 1049, and the image by the user selected in the field 'actie_foto' is loaded perfectly. But when I want to reuse this code on the contact page, no image is shown. I mean, I just see this:
An empty img src on the contact page while I'm using the exact same code for the contact page, it's just on an other page!
What am I doing wrong here?
Thanks in advance!
Hi Peter,
It looks like you are getting "myNode", but then not using it. Here's maybe another way to try this:
And there is actually another way to do this using "recursive" properties, which basically means, if the property can't be found on the current node, then keep stepping up the ancestor chain until a node is found that does have a value. I actually prefer this approach for things like a default image, which the site owner can maybe change for just a certain section in one place, but not needed to be set on every single page. That would look like this:
Good luck!
Hi Funka!,
Thank you so much! Exactly what I was looking for!
With your code I even managed to grab a few paragraphs and display them on an other page as well!
Thanks again!
is working on a reply...