forum

Home / DeveloperSection / Forums / How to Refresh dropdownlist but keep old selected value JSON

How to Refresh dropdownlist but keep old selected value JSON

Anonymous User 1647 16-Jan-2015

I have some javascript code in one of my CSHTML files

$.getJSON("@Url.Action("GetLanguages")", function (data) {
        debugger;
        var $languages = $('.supplier-languages select');
        $languages.children().filter(function () {
            return $(this).attr('value');
        }).remove();
        $.each(data, function () {
            debugger;
            var option = $('<option />', {
                text: this.Text,
                value: this.Value
            });
 
            $languages.append(option.clone());
        });
    });
    jSnap.modals.removeModal();

basically the data is a list of selectlistitems, the first part looks for all of the supplier-languages div and selects the select, we then remove all of the listitems inside of the dropdownlist which has a value (so the "Please select..." default listitem I have, will not be removed as it has no value assigned), once all the populated items have been removed, I then iterate over my data variable and populate all of my dropdownlists again.

This code works fine, but the problem I am having is, the user goes to the page and selects some options, the user then adds a new option to the dropdownlist via a new opened jquery modal, when the modal is closed the code above is than ran, but not the options they had selected are now set back to please selecte. How can I keep the old values selected and place them back into my dropdownlist options?


Updated on 16-Jan-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By