Copied to clipboard

Flag this post as spam?

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


  • Bunnynut 136 posts 318 karma points
    Dec 19, 2016 @ 12:54
    Bunnynut
    0

    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 136 posts 318 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 1082 posts 6446 karma points MVP 5x 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 136 posts 318 karma points
    Dec 21, 2016 @ 09:30
    Bunnynut
    1

    Hi Dennis,

    Thanks a lot that really helped me!

  • Dennis Adolfi 1082 posts 6446 karma points MVP 5x c-trib
    Dec 21, 2016 @ 10:21
    Dennis Adolfi
    0

    Awesome!! Glad I could help!

    Have a great day!

Please Sign in or register to post replies

Write your reply to:

Draft