Get option values from TextOption field

I’m trying to get the values from TextOption but it just returns null.
I’ve set it up in AcceleratorWebsiteFieldDefinitionSetup and added some options to it.

When i’m trying to get the values in my ViewModelBuild with website.Fields.GetValue<IList<string>>(PageFieldNameConstants.MyTextOptionField, CultureInfo.CurrentUICulture); it just returns null.

(website is _requestModelAccessor.RequestModel.WebsiteModel)

Litium version: 7

Is the field multi-culture? Otherwise, you should omit CultureInfo.CurrentUICulture.

You can also use the WebsiteModel directly, like
websiteModel.GetValue<IList<string>>(PageFieldNameConstants.MyTextOptionField) and it will check with and without culture automatically.

Yeah it is.

So something like this?
MyViewModel Build(WebsiteModel websiteModel) { model.MyStringList = websiteModel.GetValue<IList<string>>PageFieldNameConstants.MyTextOptionField) }

Yes, just missing a ( before the constant.

But if the field is set to multi culture, have you double-checked that there is a value for the language you want?

Yeah there’s a value for every language i have. In the pages that uses a selected value it works.
But not here when i want to get all the values

Do you want to get all the possible values for the field?
And not the selected values for the field on the current website?

If so, then you should get the definition of the field instead. Something like:

var field = _fieldDefinitionService.Get<WebsiteArea>(PageFieldNameConstants.MyTextOptionField);
var options = field.Option as TextOption;
var values = options.Items.Select(option => option.Value);

Ah perfect, that was exactly what i needed!
Thank you and have a great weekend :slight_smile:

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.