I have tried an example: this example
and its working fine but when I tried to bind dynamically it's not working. Please anyone can help in this.
please find below code:
Dropdown code:
<div class="col-md-4">
<div class="form-group">
<label for="field-2" class="control-label">Dropdown</label>
<select class="form-control selectpicker" multiple data-selected-text-format="count" data-live-search="true" data-style="btn-white" id="lstdrpdwn" name="lstdrpdwn">
</select>
</div>
</div>
** Jquery:**
function FillDetail(id) {
$.ajax({
dataType: "json",
type: "POST",
url: '../Getdata',
data: 'id=' + id,
async: true,
success: function (data) {
$.each(data, function (i, item) {
alert('[' + item.CooperativeUserID + ']');
$('#lstdrpdwn').selectpicker('val', '[' + item.CooperativeUserID + ']'); //.val(item.CooperativeUserID);
$('#lstdrpdwn').selectpicker('refresh');
})
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
}
Also i have tried to remove //$('#lstdrpdwn').selectpicker('refresh');
the lines.
And aslo tried to refresh the selectpicker
$('.selectpicker').selectpicker('refresh');
its not working.
When I bind the values with static IDs it worked fine at the time of filling dropdown.
function filldropdown() {
return $.ajax({
dataType: "json",
type: "POST",
url: '../Getdropdown',
data: 'id=0',
async: true,
success: function (data) {
$("#lstdrpdwn").html("");
$.each(data, function (key, val) {
$("#lstdrpdwn").append($("<option></option>").val(val.ID).html(val.Name));
});
$('#lstdrpdwn').selectpicker('refresh');
$('#lstdrpdwn').selectpicker('val', [2,5]);
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
}