Displaying media image from a child node in partial views
I'm using the tried and tested code below to display a media image in parial view but does anyone know how I can reference the same media image from a parent node?
I ma currrently displaying the image (successfully) in a blog article but also wish to display it in the parent article listings.
@{
if (post.HasValue("mainImage")){
var dynamicMediaItem = Umbraco.Media(CurrentPage.mainImage);
I assume that you are trying to create a listview over your blogposts, where you example pull out the headline teaser, and then the images, so the user can click read more.
The way I think that you can display image from a child node is like this,
@* Ensure that the Current Page has children, where the property umbracoNaviHide is not True *@
Thanks for the input, unfortunately this didn't work either. I'm not fussed with the solution being a media picker, I would actually prefer if it is was using upload... Is this solution any simpler?
Still no joy, but I have just realised something that to be honest should have occured to me before. The page where I am trying to display the image is 4 levels above where the article itself sits, will this have a bearing on teh code if I was using your original solution of
@*Ensure that the CurrentPage has children,where the property umbracoNaviHide isnotTrue*@
Just to be sure, you are setting the image on the December Test Post, node and what to use the same image on a list that list all the posts. It is on the December node that you want the image from the posts, to appear or is it on the 2014 too.
Looking forward to hear from you, then I will do my best to help you out if I can.
That is correct. I have successfully didsplayed the image on the post itself using the code below so this is fine. The challenge I am having is displaying the same image (for each post) in a list view that is appearing on the 'News' node. So the code to display the image on the news page needs to reference an image 4 levels down
News
Posts
2014
December
December Test Posts
This is the code that is successfully displaying the image on the Post itself (I have reverted back to using media picker)
if (CurrentPage.HasValue("mainImage")){
var dynamicMediaItem = Umbraco.Media(CurrentPage.mainImage);
I think that I found the way where you can display the image from your blogpost on the news page
I have made my example using the Upload property editor. First you need to find the file SmartListPosts.cshtml in the Settings section under partial views. In this file, you need to add this line to display the image.
<img src="@post.GetPropertyValue("mainImage")" />
In my case the alias of the upload type is mainImage, remember to change this so it match your setup.
You need to add the image in this foreach block.
@foreach (IPublishedContent post in results.Skip(skip).Take(itemsPerPage)) {
Huzzah! that worked beautifully and so simple it was embarrasing! Thanks Dennis.
Ok, one last bit, I now still can't get the upload version to show in teh post itself. I have tried using the code from the upload link you sent but couldn't get that to work at all so I tried the line below, still with no joy.
I'm a self taught coder (as you can probably tell!) and slowly learning all the trickier in's and out's of Umbraco and just how powerful it is; can you recommend any reference material, sites or books to help further my knowledge??
Thanks again chap and have a great holiday over the next couple of weeks.
Displaying media image from a child node in partial views
I'm using the tried and tested code below to display a media image in parial view but does anyone know how I can reference the same media image from a parent node?
I ma currrently displaying the image (successfully) in a blog article but also wish to display it in the parent article listings.
@{
if (post.HasValue("mainImage")){
var dynamicMediaItem = Umbraco.Media(CurrentPage.mainImage);
<img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>
}
}
Hi Brian,
You can use Children Property of parent node.
Thanks
Hi Alex,
Thanks for the info, I did think that was the idea but (and here's where I sound like a newby) I'm not sure of the exact code to use
Is it to replace 'CurrentPage" ?
Brian,
You can try like that :
Umbraco.AssignedContentItem.Children
Thanks Alex,
I've tried
@{
if (post.HasValue("mainImage")){
var dynamicMediaItem = Umbraco.AssignedContentItem.Children.Media(CurrentPage.mainImage);
<img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>
}
}
and
@{
if (post.HasValue("mainImage")){
var dynamicMediaItem = Umbraco.AssignedContentItem.Children(CurrentPage.mainImage);
<img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>
}
}
But neither of them are working.
Can you see where I'm going wrong?
what does "post" mean?
Sorry, that was me forgetting it was there... was just playing with various bits when I'd copied that.
The version I tried was the same as above (using your code) but 'CurrentPage' instead of post
Hi Brian,
I assume that you are trying to create a listview over your blogposts, where you example pull out the headline teaser, and then the images, so the user can click read more.
The way I think that you can display image from a child node is like this,
Hope this helps,
/Dennis
Hi Dennis,
Thanks for the input, unfortunately this didn't work either. I'm not fussed with the solution being a media picker, I would actually prefer if it is was using upload... Is this solution any simpler?
Thanks
Brian
Hi Brian,
Have you seen the documentation on using the upload field. http://our.umbraco.org/documentation/using-umbraco/backoffice-overview/property-editors/Built-in-Property-Editors/Upload. Only focus on the strongly typed way or the dynamic razor MVC examples.
Hope this helps,
/Dennis
Still no joy, but I have just realised something that to be honest should have occured to me before. The page where I am trying to display the image is 4 levels above where the article itself sits, will this have a bearing on teh code if I was using your original solution of
Hi Brian,
Just to be sure, you are setting the image on the December Test Post, node and what to use the same image on a list that list all the posts. It is on the December node that you want the image from the posts, to appear or is it on the 2014 too.
Looking forward to hear from you, then I will do my best to help you out if I can.
/Dennis
Hi Dennis..
That is correct. I have successfully didsplayed the image on the post itself using the code below so this is fine. The challenge I am having is displaying the same image (for each post) in a list view that is appearing on the 'News' node. So the code to display the image on the news page needs to reference an image 4 levels down
Hi Brian,
I think that I found the way where you can display the image from your blogpost on the news page
I have made my example using the Upload property editor. First you need to find the file SmartListPosts.cshtml in the Settings section under partial views. In this file, you need to add this line to display the image.
In my case the alias of the upload type is mainImage, remember to change this so it match your setup.
You need to add the image in this foreach block.
Hope this helps,
/Dennis
Huzzah! that worked beautifully and so simple it was embarrasing! Thanks Dennis.
Ok, one last bit, I now still can't get the upload version to show in teh post itself. I have tried using the code from the upload link you sent but couldn't get that to work at all so I tried the line below, still with no joy.
<img src="GetPropertyValue("mainImage")" />
Is this close though?? #soclose!
Hi Brian,
For the blogpost it self you need to do it like this way. Again I am using the Upload property editor.
The file you need to add this in is: SmartShowPost.cshtml
Hope this helps,
/Dennis
Dennis,
If you were in front of me now I'd kiss you!
I'm a self taught coder (as you can probably tell!) and slowly learning all the trickier in's and out's of Umbraco and just how powerful it is; can you recommend any reference material, sites or books to help further my knowledge??
Thanks again chap and have a great holiday over the next couple of weeks.
Bri
Hi Brian,
Glad that I could help you out, and have a great holiday over the next couple of weeks too. He Brian do you see the Umbraco TV, here is http://umbraco.tv/ a good place to start. Some of the videos are free to watch, and some needs that you subscribe to the service. Other good stuff is http://our.umbraco.org/documentation/Using-Umbraco/Creating-Basic-Site/ and from this year chrismas advent http://24days.in/umbraco/2014/how-to-set-up-an-umbraco-site/
For the Razor thing take a look that these links: http://our.umbraco.org/documentation/Reference/Mvc/partial-views and http://our.umbraco.org/documentation/Reference/Templating/Macros/Partial-View-Macros/ Another help to understand Razor could be the cheatsheet made by Peter Gregory http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets
I know the umbraco-v6-mvc-razor-cheatsheets says for version 6 but you should be able to use the same syntax for Umbraco 7.
Hope this helps,
/Dennis
is working on a reply...