Copied to clipboard

Flag this post as spam?

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


  • Bunnynut 129 posts 311 karma points
    Dec 19, 2016 @ 12:54
    Bunnynut
    0

    Show umbraco image

    I have been searching for one simple thing and that is showing an image in my razorview. For some reason Umbraco.Media doesn't exist, can somebody help? I'm running Umbraco 7.5.6

  • Bunnynut 129 posts 311 karma points
    Dec 20, 2016 @ 20:30
    Bunnynut
    0

    Can somebody explain what the best way is to get the url of an image by a nodeid?

  • Dennis Adolfi 1072 posts 6378 karma points MVP 2x c-trib
    Dec 21, 2016 @ 07:27
    Dennis Adolfi
    100

    Hi Bunnynut.

    The Umbraco.Media(int) is a UmbracoHelper and in order for you to be able to use it using the "Umbraco" prefix your view needs to inherit either UmbracoTemplatePage or UmbracoViewPage. See example:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<IPublishedContent>
    @{
        Layout = null;
    
        var mediaId = 1107;
        var mediaItem = Umbraco.Media(mediaId);
    }
    
    <img src="@mediaItem.Url"/>
    

    Now, if you dont want you view to inherit from UmbracoTemplatePage or UmbracoViewPage that's fine, but then you need to create you own instance of the UmbracoHelper and use it instead of the "Umbraco" prefix. See example:

    @{
        Layout = null;
    
        var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
        var mediaId = 1107;
        var mediaItem = umbracoHelper.Media(mediaId);
    }
    
    <img src="@mediaItem.Url"/>
    

    Good luck!!

  • Bunnynut 129 posts 311 karma points
    Dec 21, 2016 @ 09:30
    Bunnynut
    1

    Hi Dennis,

    Thanks a lot that really helped me!

  • Dennis Adolfi 1072 posts 6378 karma points MVP 2x c-trib
    Dec 21, 2016 @ 10:21
    Dennis Adolfi
    0

    Awesome!! Glad I could help!

    Have a great day!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies