How to display multiple selections in Editor Selectize from data source?
How to display multiple selections in Editor Selectize from data source?
![mrsearing-all](https://secure.gravatar.com/avatar/07f833c5d8bc8ab5a04cf4383caf908c/?default=https%3A%2F%2Fvanillicon.com%2F07f833c5d8bc8ab5a04cf4383caf908c_200.png&rating=g&size=120)
I am using Editor with Selectize, which loads the options as expected and I can successfully select multiple options and save to the database. However, when I go to edit that record, specifically the field 'Brands' below, the Selecitze Editor field is blank. If it is just one, then it successfully shows that option as selected.
I've tried creating a custom GetFormatter and the ready-to-use Format methods for the field.
With the custom GetFormatter, I receive the error 'Unable to cast value to be String'
.Field(new Field("Brands")
.GetFormatter((val, data) => val == null ? Format.NullEmpty() : Format.Explode(","))
.SetFormatter(Format.NullEmpty()))
Using the ready-to-use Format methods does not seem to alter the data and there is no error, however Selectize does not recognize multiple selections and does not display if there is more than one option selected.
.Field(new Field("Brands")
.GetFormatter(Format.Explode(","))
.GetFormatter(Format.NullEmpty())
.SetFormatter(Format.NullEmpty()))
What is returned with no Formatters or ready-to-use methods:
Brands: "152" //will displayed as selected, but not "152,232"; shows as empty. Can be null
Comments: null
ContactID: 1498
Email: "test@test.com"
FirstName: "Test"
LastName: "Contact"
Notify: false
Title: "Test Title"
Updated: "2023-11-29T18:13:51.703"
Unfortunately I cannot link to the page but can provide any clarification.
I have read this discussion https://datatables.net/forums/discussion/68294/how-to-show-multiple-selected-options#latest so I know it needs to be an array but cannot get the format in the server-side code to do so.
This question has an accepted answers - jump to answer
Answers
It looks like I may need to use Field.GetValue to set a delegate, but I am not quite sure how to use this. I have searched the forums and other sites for any examples, but have not found a decent example of how it used.
For anyone looking for the same solution, this is how I solved it:
And change the property in my class from
string
tostring[]
For it to save properly, use
Nice, thanks for posting the solution!
Colin