Copied to clipboard

Flag this post as spam?

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


  • Andreas Pfanner 196 posts 314 karma points
    Jun 12, 2015 @ 14:17
    Andreas Pfanner
    1

    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

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jun 12, 2015 @ 15:30
    Jamie Pollock
    101

    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 :)

    Thanks,
    Jamie

  • Andreas Pfanner 196 posts 314 karma points
    Jun 15, 2015 @ 14:24
    Andreas Pfanner
    0

    Hi Jamie,

    thanks for your great post! I got it working.

    Best Regards Andreas

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jun 15, 2015 @ 14:49
    Jamie Pollock
    0

    My pleasure, mate :)

Please Sign in or register to post replies

Write your reply to:

Draft