Return comma separated string as multiple options in select dropdown

Hi,

I’ve used the select 2 multiple dropdown list to store a comma separated string in the db. example: dropdown list has 1.NONE 2.GREEN 3.YELLOW 4.RED in it. If you select GREEN and YELLOW, it will store in the db as GREEN,YELLOW.

NOW the user needs to be able to edit that so when you get that information back from the db, it only returns GREEN and not the second option being YELLOW.

    $(document).ready(function () { $("#RiskRatingTypes").select2({ multiple: true, placeholder: "Select", allowClear: true}); });

I’ve found the answer.
I’ve returned the entry from the db to the view in a hidden field and added it as an array to the select drop:

    var items = ($('#riskratingfields').val().split(',')); // this is the hidden field which shows as NONE,GREEN
    $(document).ready(function () {
        $("#RiskRatingTypes").select2({
            multiple: true,
        });
        $('#RiskRatingTypes').val(items).trigger('change');
    });