2

I am working using Bootstrap's selectpicker plugin. When the CountryList changes, I dynamically select the existing cities of that country in the database and transfer the data to the CityList. But even if the first option is selected, the header of selectpicker says nothing selected. And if there is 1 city in the database of the selected country, nothing selected does not change even if that city is clicked. Actually, I can get the value of city coming selected, but it says nothing selected. How can I solve this?

Selectpicker SS : selectpicker screenshot

Response SS: JsonResponse

Thank you very much in advance for your answers.

CountryList change function

$("#CountryList").change(function() {
            var countryId = $('#CountryList option:selected').val();
            $.ajax({
                type: "POST",
                url: "@Url.Action("GetCities", "Project")",
                data: { countryId: countryId },
                dataType: "text",
                success:
                    function(response) {
                        var parsedResponse = jQuery.parseJSON(response);
                        console.log(parsedResponse);
                        var option = "";
                        for (var i = 0; i < parsedResponse['cityList'].length; i++) {
                            if (i == 0) {
                                var city = parsedResponse['cityList'][i].text;
                                var cityId = parsedResponse['cityList'][i].value;
                                option += `<option selected value="${cityId}">${city}</option>`;
                            } else {
                                var city = parsedResponse['cityList'][i].text;
                                var cityId = parsedResponse['cityList'][i].value;
                                option += `<option value="${cityId}">${city}</option>`;
                            }
                        }
                        $("#CityList").html(option);
                        $("#CityList").selectpicker('refresh');
                    }
            });
        });

Project Controller GetCities Action

[HttpPost]
        public async Task<JsonResult> GetCities(int countryId)
        {
            var model = new AddProductViewModel();
            var cityList = await _cityService.GetAllAsyncByCountryId(countryId);
            model.CityList = new SelectList(cityList, "Id", "Name");
            var firstCity = cityList.First();
            model.CountryDefaults = await _countryDefaultService.GetAllAsyncByCountryCityId(countryId, firstCity.Id);
            model.CountryDefault = _countryDefaultService.GetByCountryCityId(countryId, firstCity.Id);
            JsonConvert.SerializeObject(model);
            return Json(model);
        }
  • you can try and store the selected value in a variable and use `jQuery.selectpicker.util.change( $("#CityList"), preSelectedValue ); ` to set the option in the selectpicker – Prikesh Savla Apr 21 '21 at 15:04
  • Sorry, I tried the method you said and it didn't work. – murateksiolu Apr 21 '21 at 15:56
  • https://stackoverflow.com/questions/14804253/how-to-set-selected-value-on-select-using-selectpicker-plugin-from-bootstrap Maybe the issue is in how you set the selected value for the select box originally `$('#CityList').val(1);` before selectpicker refresh – Prikesh Savla Apr 21 '21 at 16:11
  • Thank you for help. I mean Actually, I can get the value of city coming selected, but it says nothing selected. How can I solve this? – murateksiolu Apr 26 '21 at 13:19
  • can you update the code or add it in a fiddle? – Prikesh Savla Apr 26 '21 at 13:38

1 Answers1

1

First we need to append the options in dropdown and refresh the selectpicker, now we have to dynamically select the first option and again refresh the selectpicker. That how selectpicker is working.

Check the below code

var option = "";
for (var i = 0; i < parsedResponse['cityList'].length; i++) {
    var city = parsedResponse['cityList'][i].text;
    var cityId = parsedResponse['cityList'][i].value;
    option += `<option value="${cityId}">${city}</option>`;
}
$("#CityList").html(option);
$("#CityList").selectpicker('refresh');
$("#CityList").val($('#CityList option:first').val());
$("#CityList").selectpicker('refresh');
parth
  • 1,803
  • 2
  • 19
  • 27