How to SetValue for a Dropdownlist value programmatically in Umbraco 8
I am trying to use SetValue on IContent to programmatically set the value of a dropdown list.
I can set the value of Textboxes no issue but not Dropdownlists.
I have seen solutions for version 7 but I can't find anything for version 8
private void SetDropDownListValue(IContent content, int dropDownListDataTypeId, string propertyName, string prevalue)
{
var dropdown = _DataTypeService.GetAll().First(x => x.Id == dropDownListDataTypeId);
IDataType dataType = _DataTypeService.GetDataType(dropdown.Id);
//valueList looks good, can see all prevalues
ValueListConfiguration valueList = (ValueListConfiguration)dataType.Configuration;
ValueListItem item = valueList.Items.SingleOrDefault(i => i.Value == prevalue);
if (item != null)
{
//item.Id looks good, a simple integer but content crash in backoffice
//item.Value looks correct too
content.SetValue(propertyName, item.Id);
}
}
I get this in the backoffice when viewing this content
How to SetValue for a Dropdownlist value programmatically in Umbraco 8
I am trying to use SetValue on IContent to programmatically set the value of a dropdown list. I can set the value of Textboxes no issue but not Dropdownlists.
I have seen solutions for version 7 but I can't find anything for version 8
I get this in the backoffice when viewing this content
Version 7 solution: https://our.umbraco.com/forum/developers/api-questions/54462-Set-value-of-UmbracoDropDown-programatically
I worked out the problem. SetValue has to be called like this:
Little bit better:
is working on a reply...