Copied to clipboard

Flag this post as spam?

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


  • Mikael Ekström 14 posts 114 karma points
    Sep 17, 2017 @ 13:53
    Mikael Ekström
    0

    Cant retrieve Umbraco.Media in macro code

    Hello.

    I am trying to implement a slider and have gotten almost to the finish line but i have a major issue that i cant get past.

    When trying to get the image from Umbraco using the Umbraco.Media() method i get nothing back.

    Here is the code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        //get all children of type 'slide_item
        var slideId = Model.MacroParameters["sliderId"];
        var node = Umbraco.Content(slideId);
        var slides = node.Children.Where("DocumentTypeAlias == \"slide_item\"");
    }
    
    <div class="slider">
        @foreach (var slide in @slides) {
          //Fallback: Ignore slide when no image was uploaded,
          //else the slide would cause a server error.
          if(@slide.slideImage != null) {
            var image = Umbraco.Media(@slide.slideImage);
            <div class="slide">
                <img alt="@slide.name" src="@image.Url">
            </div>
          }
        }
    </div>
    

    For some reason the var image = Umbraco.Media(@slide.slideImage); call does not return anything so all i get right now is the alt text.

    I can print @slide.slideImage and see the configured id printed, so the image exists and the id is there.

    Any idea of what can be the problem?

  • pbl_dk 150 posts 551 karma points
    Sep 17, 2017 @ 19:02
    pbl_dk
    101

    If I use GetType on the slide.slideImage umbraco tells me: Umbraco.Web.PublishedContentModels.Image

    So you should try

    var image = Umbraco.Media(slide.slideImage.ToString());
    

    or you could use

    slide.slideImage.Id
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 17, 2017 @ 21:48
    Alex Skrypnyk
    0

    Hi Mikael

    What version of Umbraco are you using?

    Are Property value converters enabled? If yes, then use code that Elitenet provided.

    Thanks,

    Alex

  • Mikael Ekström 14 posts 114 karma points
    Sep 18, 2017 @ 07:43
    Mikael Ekström
    0

    I found the .toString() solution after posting but forgot to update the post. Problem solved.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Sep 18, 2017 @ 08:03
    Alex Skrypnyk
    0

    Glad that you solved the issue!

Please Sign in or register to post replies

Write your reply to:

Draft