Copied to clipboard

Flag this post as spam?

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


  • ggesheva 14 posts 114 karma points
    Sep 12, 2021 @ 19:06
    ggesheva
    0

    Rendering image from member's property media picker

    Hi there!

    After setting up coustom routing for members profile pages I want to display all the data they have as member properties.

    Some of those properties are Images selected from Media Pickers. memberProfile is from Umbraco.Core.Models.Member.

    @if (memberProfile.HasProperty("photo")) 
    { 
       var photo = memberProfile.GetValue<IPublishedContent>("photo"); 
       <img class="about-image" src="@photo.Url()" alt="@memberProfile.Name"> 
    }
    

    I made sure there is an image that's already picked but the variable photo is still being left out as a null. If I remove the <IPublishedContent> the variable gets the umb value.

    How could I solve this issue?

  • Thomas Kassos 54 posts 263 karma points
    Sep 13, 2021 @ 01:07
    Thomas Kassos
    0

    Hi ggesheva, I am not sure what exactly is the issue but here are 3 things to try.

    First in case you are using a the MediaPicker2 with Multiple enabled, try something like this

      @if (memberProfile.HasProperty("photo")) 
    { 
       var photo = memberProfile.GetValue<IEnumerable<IPublishedContent>>("photo").FirstOrDefault(); 
       <img class="about-image" src="@photo.Url()" alt="@memberProfile.Name"> 
    }
    

    and if its the new MediaPicker3 try something like this

    Multiple enable:

      @if (memberProfile.HasProperty("photo")) 
    { 
       var photo = memberProfile.GetValue<IEnumerable<MediaWithCrops>>("photo").FirstOrDefault(); 
       <img class="about-image" src="@photo.Url()" alt="@memberProfile.Name"> 
    }
    

    Multiple disabled:

      @if (memberProfile.HasProperty("photo")) 
    { 
       var photo = memberProfile.GetValue<MediaWithCrops>("photo"); 
       <img class="about-image" src="@photo.Url()" alt="@memberProfile.Name"> 
    }
    
  • Charles Séguin 1 post 21 karma points
    Nov 24, 2022 @ 21:06
    Charles S├ęguin
    0

    Hello, I have the same issue for my project on Umbraco 10. I'm trying to render the Media Picker 3 image on a page but it's being left out as a null.

    Did you solved this?

    var currentEntreprise = await _memberManager.GetCurrentMemberAsync();
    var entreprise = _memberService.GetByEmail(currentEntreprise.Email);
    
    var typedMediaPickerSingle = entreprise.GetValue<MediaWithCrops>("logo", culture);
    
    <img src="@typedMediaPickerSingle " alt="" />
    
  • Huw Reddick 1061 posts 3581 karma points c-trib
    Nov 25, 2022 @ 08:52
    Huw Reddick
    1

    should be something like

    <img src="@typedMediaPickerSingle.Url()" alt="" />
    
  • Alec D 5 posts 75 karma points
    Dec 19, 2022 @ 19:03
    Alec D
    0

    Did you get this resolved? I have the same issue. The value returns null if I try to cast it. This is the raw value:

    [{"key":"b7c81c0f-cebc-4633-9c0a-68fc05f8ab64","mediaKey":"0a1f1631-e4b0-45e1-8094-7d41e466933d","crops":null,"focalPoint":null}]
    

    What data type is this?

  • Huw Reddick 1061 posts 3581 karma points c-trib
    Dec 20, 2022 @ 08:37
    Huw Reddick
    1

    Hi Alec & Charles,

    The problem you are having is that you are not retreiving the member correctly in order to get at the image

    var currentEntreprise = await _memberManager.GetCurrentMemberAsync();
    

    returns a MemberIdentity, in order to access the image property for that member you need an IpublishedContent model, you can convert the memberidenty to a published content by calling

    var member = _MemberManager.AsPublishedMember(entreprise);
    

    now you can get the image and render it

    var typedMediaPickerSingle = member.Value<MediaWithCrops>("logo");
    
    <img src="@typedMediaPickerSingle.Url() " alt="" />
    

    Hopefully that should help.

  • Alec D 5 posts 75 karma points
    18 days ago
    Alec D
    0

    This is perfect thank you!

    Is this documented somewhere? I did try to find a solution in the Umbraco docs but I definitely didn't find anything so helpful as this.

  • Huw Reddick 1061 posts 3581 karma points c-trib
    18 days ago
    Huw Reddick
    0

    I don't think there is anything explicitly stated for a custom property on the member, but if you mark it as answered someone may find the post helpful when looking :)

  • Alec D 5 posts 75 karma points
    18 days ago
    Alec D
    0

    Would if I could but I'm not OP.

Please Sign in or register to post replies

Write your reply to:

Draft