I've added a salutation field (Data Type: Salutations - DropDown list) to my members type. The Data type is pre filled with available salutations (Mr, Mrs,..).
When I try to set the field in my register controller via
member.SetField("salutation", "Mr") I don't see the dropdown value selected in the backend.
Hi Andreas,
The built in Dropdown list data type actually saves the id of the prevalue used so if you can find that it would be want you want to set the value to.
Try the following code if you want a robust version which isn't specifically tied to IDs because these can change from environment to environment.
So basically you want to get the prevalues for the current data type, like so!
const string expectedSalutation = "Mr"; //or "Mrs", etc, etc...
const string memberTypeAlias = "member";
const string salutationPropertyTypeAlias = "salutation";
var Services = ApplicationContext.Current.Services;
var memberType = Services.MemberTypeService.Get(memberTypeAlias);
var salutationPropertyType = memberType.PropertyTypes.FirstOrDefault(propertyType => string.Equals(propertyType.Alias, salutationPropertyTypeAlias));
var preValuesForSalutationDataType =
Services.DataTypeService.GetPreValuesCollectionByDataTypeId(salutationPropertyType.DataTypeDefinitionId);
var expectedPreValue =
preValuesForSalutationDataType.PreValuesAsDictionary.FirstOrDefault(preValue =>
string.Equals(preValue.Value.Value, expectedSalutation));
var newMember = Services.MemberService.CreateMember("username", "[email protected]", "User Name", memberType);
newMember.SetValue(salutationPropertyTypeAlias, expectedPreValue.Value.Id);
Services.MemberService.Save(newMember);
The reason for using PreValuesAsDictionary is because the dropdownlist prevalues are a dictionary and the Value.Value isn't a type either, the actual prevalue is the Dictionary Value then the value of the dictionary value is the text you're looking for and have set in the backoffice.
Alternatively you try out nuPickers which can be populated by XML/JSON/standard .net and is entirely more usable than the core dropdown list (sorry core team!)
I hope this help, if you, please mark it as the solution :)
Dropdown List Data type - SetValue
Hi,
I've added a salutation field (Data Type: Salutations - DropDown list) to my members type. The Data type is pre filled with available salutations (Mr, Mrs,..).
When I try to set the field in my register controller via
member.SetField("salutation", "Mr") I don't see the dropdown value selected in the backend.
How to set this value properly?
Best Regards Andreas
Hi Andreas,
The built in Dropdown list data type actually saves the id of the prevalue used so if you can find that it would be want you want to set the value to.
Try the following code if you want a robust version which isn't specifically tied to IDs because these can change from environment to environment.
So basically you want to get the prevalues for the current data type, like so!
The reason for using PreValuesAsDictionary is because the dropdownlist prevalues are a dictionary and the Value.Value isn't a type either, the actual prevalue is the Dictionary Value then the value of the dictionary value is the text you're looking for and have set in the backoffice.
Alternatively you try out nuPickers which can be populated by XML/JSON/standard .net and is entirely more usable than the core dropdown list (sorry core team!)
I hope this help, if you, please mark it as the solution :)
Thanks,
Jamie
Hi Jamie,
thanks for your great post! I got it working.
Best Regards Andreas
My pleasure, mate :)
is working on a reply...