Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Brian 77 posts 251 karma points
    Dec 17, 2014 @ 16:27
    Brian
    0

    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"/>

         }

    }

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Dec 17, 2014 @ 17:06
    Alex Skrypnyk
    0

    Hi Brian,

    You can use Children Property of parent node.

    Thanks

  • Brian 77 posts 251 karma points
    Dec 17, 2014 @ 17:09
    Brian
    0

    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" ?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Dec 17, 2014 @ 17:16
    Alex Skrypnyk
    0

    Brian,

    You can try like that :

    Umbraco.AssignedContentItem.Children

  • Brian 77 posts 251 karma points
    Dec 17, 2014 @ 17:23
    Brian
    0

    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?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Dec 17, 2014 @ 17:53
    Alex Skrypnyk
    0

    what does "post" mean?

  • Brian 77 posts 251 karma points
    Dec 17, 2014 @ 17:58
    Brian
    0

    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 

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 17, 2014 @ 18:55
    Dennis Aaen
    0

    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,

    @* Ensure that the Current Page has children, where the property umbracoNaviHide is not True *@

    @if (CurrentPage.Children.Where("Visible").Any()){

            foreach (var blogpost in CurrentPage.Children.Where("Visible")){

                if (blogpost.HasValue("mainImage")){                                        

                    var dynamicMediaItem = Umbraco.Media(blogpost.mainImage);
                    <img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>

                }
            }
    }

    Hope this helps,

    /Dennis

  • Brian 77 posts 251 karma points
    Dec 19, 2014 @ 09:20
    Brian
    0

    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

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 19, 2014 @ 09:25
    Dennis Aaen
    0

    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

  • Brian 77 posts 251 karma points
    Dec 19, 2014 @ 09:46
    Brian
    0

    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*@

    @if(CurrentPage.Children.Where("Visible").Any()){

           
    foreach(var blogpost inCurrentPage.Children.Where("Visible")){

               
    if(blogpost.HasValue("mainImage")){                                        

                   
    var dynamicMediaItem =Umbraco.Media(blogpost.mainImage);
                   
    <img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>

               
    }
           
    }
    }

     

     

     

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 19, 2014 @ 10:06
    Dennis Aaen
    0

    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

     

     

  • Brian 77 posts 251 karma points
    Dec 19, 2014 @ 10:16
    Brian
    0

    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

    • 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);
            <img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>
        }
    Thanks for your patience and ongoing advice Dennis, much appreciated.
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 19, 2014 @ 10:39
    Dennis Aaen
    0

    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.

    <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))
                {
               
                    string author = post.GetProperty("smartBlogAuthor") != null && post.GetProperty("smartBlogAuthor").Value.ToString() != "" ?
                        post.GetProperty("smartBlogAuthor").Value.ToString() :
                        document.GetElementsByTagName("defaultAuthor")[0].InnerText;
               
                    try
                    {
                        <div class="post">
                            <h3><a href="@Umbraco.NiceUrl(post.Id)">@post.Name</a></h3>
                            @if(useSummaryOnList)
                            {
                                <p>@Html.Raw(post.GetProperty("smartBlogSummary").Value.ToString())</p>
                            }
                            else
                            {
                                @Html.Raw(post.GetProperty("smartBlogBody").Value.ToString())
                            }
                            By <a href="?author=@author">@author</a> at @Convert.ToDateTime(post.GetProperty("smartBlogDate").Value.ToString()).ToString(SmartBlogLibraries.Helpers.DateTime.DateFormatNormal)
                        </div>
                    }
                    catch(Exception) {}
                }

    Hope this helps,

    /Dennis

  • Brian 77 posts 251 karma points
    Dec 19, 2014 @ 11:13
    Brian
    0

    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!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 19, 2014 @ 11:33
    Dennis Aaen
    100

    Hi Brian,

    For the blogpost it self you need to do it like this way. Again I am using the Upload property editor.

    @if (Model.Content.HasValue("mainImage")){ 
        <img src="@Model.Content.GetPropertyValue("mainImage")" />
    }

    The file you need to add this in is: SmartShowPost.cshtml

    Hope this helps,

    /Dennis

  • Brian 77 posts 251 karma points
    Dec 19, 2014 @ 11:37
    Brian
    0

    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

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Dec 19, 2014 @ 11:59
    Dennis Aaen
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft