Copied to clipboard

Flag this post as spam?

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


  • Jordy Vialoux 73 posts 103 karma points
    Nov 18, 2014 @ 22:49
    Jordy Vialoux
    0

    @Umbraco Issues accessing Media

    Hi Guys,

    I'm having issues trying to access Media within the @Umbraco model. 

    This line of code returns me nothing but an empty src string - any ideas why?? I do have images within media, do I need to specify the folder as well? 

    <img src="@Umbraco.Media("Image")" />

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Nov 18, 2014 @ 23:35
    Jeavon Leopold
    2

    Hi Jordy,

    Do you have a media picker within your CurrentPage?

    This is how you get the media item from the picker:

    @{      
        if (CurrentPage.HasValue("mainImage")){                                         
            var dynamicMediaItem = Umbraco.Media(CurrentPage.mainImage);
            <img src="@dynamicMediaItem.Url" alt="@dynamicMediaItem.Name"/>
        }
    }
    

    Here the media picker property alias is "mainImage"

    Does this help you?

    Jeavon

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 18, 2014 @ 23:37
    Dennis Aaen
    100

    Hi Jordy,

    I assume that you are talking about that you want the background image on the body tag.

    If so the you can do it like this.

    @{ 
          var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("bodyBackgroundImage"));
    }
    <body style="background:url('@mediaItem.Url');">

    or by using this:

    @{ 
          var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("bodyBackgroundImage"));
    }
     <body style="background:url('@mediaItem.GetPropertyValue("umbracoFile")');">

    With this code you will get the background image there is set for the page that you are viewing in the browser. If you want to have one place you set the background image for the hole site e.g on the home item, you need to use the  AnscestorOfSelf axis you can read about the axis here: http://our.umbraco.org/documentation/reference/templating/macros/razor/using-razor-axes be ware that these foreach loop they are showing are using the old DynamicNode Razor.

    Another good resource is the Razor cheat sheets http://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets, I know that it´s says that it for Umbraco 6. But you can also use it for Umbraco 7, and there are all version of Razor for Umbraco. The old DynamicNode but you should not use this if you are using Umbraco 7. And there are cheat sheets for the Dynamic razor and the strongly typed razor.

    Hope this helps,

    /Dennis

  • Jordy Vialoux 73 posts 103 karma points
    Nov 18, 2014 @ 23:51
    Jordy Vialoux
    0

    DENNIS YOU BEAUTY!! Thank you so much - I used your second option. 

Please Sign in or register to post replies

Write your reply to:

Draft