Upon a model popup, I need a selectpicker dropdown to be pre-populated with values. I have tested that this method works by manually entering some IDs, like so:
$("#listPhones").selectpicker('val', [1,5]);
$("#listPhones").selectpicker("refresh");
My [WebMethod] function in the code behind returns a string like so:
1,5
And I am plugging this in using data.d, so the final output is:
$.ajax({
type: "POST",
url: "/default.aspx/GetRespIds",
data: JSON.stringify({ Responders: editPhonetbxValue }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//alert(data.d);
$("#listPhones").selectpicker('val', [data.d]);
$("#listPhones").selectpicker("refresh");
}
});
Entering the 1,5 value manually works and selects the items in the dropdown as expected, but parsing it through the AJAX data.d variable does not - can anyone point me in the right direction?
If you method return a string and you want an array you must use split. Also you have to parse it to int. Try like:
$("#listPhones").selectpicker('val', data.d.split(',').map(Number));