Copied to clipboard

Flag this post as spam?

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


  • David 29 posts 200 karma points
    Feb 21, 2019 @ 14:11
    David
    0

    Populating Media Picker Value depending on value of another field

    Afternoon all,

    Trying to set the media pickers value here. I have a drop down list (gender) which has the two options.

    I am trying to achieve the following:

    If the media picker control for that team member is empty and they are "Male", set the default value of the Media picker to a certain image. If Female do the same but different image.

    This is what I have in my controller. Can anyone point out if I am going down the right lines or not?

        public class TeamController : SurfaceController
    {
    
        private const string PartialViewFolder = "~/Views/Partials/Team/";
    
        public ActionResult RenderTeamMembers()
        {
    
            List<TeamMembers> model = new List<TeamMembers>();
            IPublishedContent teamPage = CurrentPage.DescendantOrSelf();
    
            foreach (IPublishedContent page in teamPage.Children)
            {
                int imageId = page.GetPropertyValue<int>("personPhoto");
                var mediaItem = Umbraco.Media(imageId);
                var gender = page.GetPropertyValue<string>("personGender");
    
                if(mediaItem == null)
                {
    
                    if (gender == "Male")
                    {
                        mediaItem.SetValue("personPhoto", 1150);
                    }
    
                    else if (gender == "Female")
                    {
                        mediaItem.SetPropertyValue(1151);
                    }
    
                }
    
                model.Add(new TeamMembers(page.GetPropertyValue<string>("personGender"), page.Name, page.GetPropertyValue<string>("personBio"), page.GetPropertyValue<string>("personRole"), mediaItem.Url, page.Url));
            }
    
            return PartialView(PartialViewFolder + "_TeamMembers.cshtml", model);
        }
    
    }
    
  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Feb 21, 2019 @ 23:36
    Alex Skrypnyk
    0

    Hi David

    In controllers use Content Service for manipulating content in database.

    https://our.umbraco.com/documentation/Reference/Management/Services/ContentService/

    Use media service for storing new media from the file - https://our.umbraco.com/documentation/reference/management/services/mediaservice/

    Thanks

    Alex

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Feb 25, 2019 @ 16:15
    Alex Skrypnyk
    0

    Hi David

    Did you find a solution for this topic? Can you share with our community?

    Alex

  • David 29 posts 200 karma points
    Mar 05, 2019 @ 09:17
    David
    0

    I had a read but didn't really understand what I needed to put. The placeholder icons already exist, so don't need to go down the route of creating the media Item. I just need to reference them and save them but didn't have any luck in doing so.

    Any suggestions?

Please Sign in or register to post replies

Write your reply to:

Draft