Copied to clipboard

Flag this post as spam?

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


  • steven smith 20 posts 130 karma points
    Jun 26, 2014 @ 16:56
    steven smith
    0

    Recursive Image from media picker

    Using MS visual studio 2012, c# 4.5 and Umbraco 7.1.

    I have been tweaking my master page, and i noticed i had to make images recursive (display on every page).

    with property's its no problem, you simply say for example:

    @Umbraco.Field("pageTitle", recursive: true)

    this works fine, however for images its a little different as im using a media picker as the datatype for the image, when you do this you need to add some extra code like the following

    if (Model.Content.HasValue("titleImage"))
    {
        var dynamicMedia1 = Umbraco.Media(Model.Content.GetPropertyValue("titleImage"));
        <a class="pull-left">
            <img class="media-object" src="@dynamicMedia1.umbracoFile" alt="" />
        </a>
    }

    this display the image correctly which is great, but as it does not use umbraco.field i cant see how to use "recursive". I've tried looking at some old documentation but doesn't really reference images. And as most will know the new info Isn't quiet available + from what i found there was no mention of recursive images.

    Is this possible? Am i missing something?

    cheers guys

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 26, 2014 @ 17:58
    Dennis Aaen
    2

    Hi Steven,

    The way you can make a property recursive in Umbraco is by adding a underscore in the front of your ._propertyAlias. Some something like this will make it recursive.

    You could try something like this:

    if(Model.Content.HasValue("_titleImage"))
    {
       
    var dynamicMedia1 =Umbraco.Media(Model.Content.GetPropertyValue("titleImage"));
       
    <a class="pull-left">
           
    <img class="media-object" src="@dynamicMedia1.umbracoFile" alt=""/>
       
    </a>
    }

    Maybe you don't need the if statement since you go recursively up through the content tree, until you find an image.

    var dynamicMedia1 =Umbraco.Media(Model.Content.GetPropertyValue("_titleImage"));
    <a class="pull-left">
       
    <img class="media-object" src="@dynamicMedia1.umbracoFile" alt=""/>
     
    </a>

    If you are new to razor there a some good resources I think could be nice to know. e.g the cheatsheets

    http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

    If you´re using Umbraco 7, you can still use the cheatsheets, as far as I know there isen´t the big difference in Umbraco 6 and 7 razor, if any.

    http://our.umbraco.org/documentation/Reference/Templating/Mvc/

    Hope this helps,

    /Dennis

  • Lars 66 posts 136 karma points
    Mar 30, 2015 @ 20:54
    Lars
    0

    Hi Dennis

    I am sorry. Putting an underscore in front of the alias is not making the result of the Image Picker recursive. Umbraco can't find the image then. "myImage" is ok but "_myImage" is not working.

    Am I missing a setting or??

    //Lars

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 30, 2015 @ 21:05
    Dennis Aaen
    2

    Hi Lars,

    What if you do something like this would it work for you?.

    @{
        if(Model.Content.HasValue("mainImage", true)){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImage"));
            <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>   
        }  
    }

    /Dennis

  • Lars 66 posts 136 karma points
    Mar 31, 2015 @ 09:14
    Lars
    0

    Hi Dennis

    Thank you for your answer.

    Yes it works but with a twist. I need to put "true" in 2 places. One like you suggest and another at the other place where "mainImage" is mentioned. This is the code that works for me. Please notice the places with TRUE.

    // Lars

    @{
       
    if(Model.Content.HasValue("mainImage", TRUE)){
           
    var mediaItem =Umbraco.TypedMedia(Model.Content.GetPropertyValue("mainImage". TRUE));
           
    <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>    
       
    }  
    }
  • Harish 15 posts 74 karma points
    Apr 13, 2015 @ 11:07
    Harish
    0

    Hi Dennis/Lars, 

    Thanks for your help recursive image worked for me. But my code is bit different from yours. May be this below code mite help some person.

        if (Model.Content.HasValue("Image", true))

               {

                   var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("Image", true));                    

                   <img src="@mediaItem.GetPropertyValue("umbracoFile")"  />

                }

    Thanks

  • Lars 66 posts 136 karma points
    Apr 14, 2015 @ 09:09
    Lars
    0

    You're welcome :-)

Please Sign in or register to post replies

Write your reply to:

Draft