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);
}
}
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.
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?
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
Hi David
Did you find a solution for this topic? Can you share with our community?
Alex
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?
is working on a reply...