get custom choicefield in view for registration and edit pro

Posted by laura@refactoredmedia.com on 20-Feb-2020 16:16

I added some custom fields to the user profile that utilize the choice field for checkboxes and dropdown lists. I have been unable to find documentation on how to get these fields for the user profile. I keep getting directed to 

https://knowledgebase.progress.com/articles/Article/how-to-work-with-basic-profile-custom-fields-in-mvc-profile-and-registration-widgets which only shows how to get text box fields, not choice field vales to select

and https://knowledgebase.progress.com/articles/Article/mvc-how-to-display-custom-choicefield-s-selected-choices-using-razor and https://www.progress.com/documentation/sitefinity-cms/use-custom-fields-in-widget-templates  only show how to get choicefield values for custom widgets but Model.Items are not available to me since I am working with the RegistrationViewModel and ProfileEditViewModel which only has @Model.Profile["FieldName"] available. 

So, how exactly do I get the custom choice fields values for the user profile so that when users register they can select from the options I entered into the admin user profile interface?

All Replies

Posted by laura@refactoredmedia.com on 26-Feb-2020 16:32

Thanks to Nevena at support:

The code sample below is the way to set the values. For persisting the values in the database, it is needed to save the changes by using the UserProfileManager:

UserProfileManager profileManager = UserProfileManager.GetManager();

.......

profile.SetValue("Checkboxes", new string[] { "Option2" } );

......

profileManager.SaveChanges();

However, it is a good practice to execute the logic in the controller and keep the view only for displaying the value.

Here is a code sample to display the custom field values in the view after they have been set:

@foreach (var choice in Model.Item.Fields.Choices) //Choices is a custom ChoiceField added to a content type

{

<span>@choice.Text.ToString()</span> :

<span>@choice.PersistedValue.ToString()</span>

}

For more details, please check this KB article - knowledgebase.progress.com/.../mvc-how-to-display-custom-choicefield-s-selected-choices-using-razor

This thread is closed