Hello - my boss wants me to implement select2 for a new feature. I’m having a weird issue where it’s passing null params to the controller for filtering. I’ve tried your syntax and the other solutions on stackoverflow, but no dice. Here’s the JS, i included both ajax calls I’ve tried. We also tried hard-coding the params:
$(".filter-advertisers").select2({
//data: filterSelect,
ajax: {
url: 'Home/GetFilterAdvertiser',
data: function (params) {
var query = {
search: params.term,
}
return query
},
processResults: function (data) {
//advertisers = $.map(data, function (obj) {
// obj = { id: i, text: obj }
// i = i + 1;
// return obj
//})
return {
results: data
}
}
},
placeholder: 'Advertisers',
closeOnSelect: false,
minimumInputLength: 4,
allowClear: true,
width: '150',
multiple: true
});
$(".filter-agencies").select2({
ajax: {
cache: false,
datatype: 'JSON',
type: 'GET',
url: 'Home/GetFilterAgency',
data: function (params) {
return {q:params}
//var query = {
// search: params.term
//}
//return query
},
processResults: function (data) {
return {
results: data
}
}
},
placeholder: 'Agencies',
width: '150',
multiple: true,
closeOnSelect: false,
minimumInputLength: 4
//tags: true
});
C#:
[HttpGet]
public string GetFilterAdvertiser(string query)
{
var x = _orderedLinesProcessor.GetFilterAdvertiser();
var i = 0;
var dict = new Dictionary<int, string>();
foreach (var el in x)
{
dict.Add(i, el);
i += 1;
}
return JsonConvert.SerializeObject(dict);
}
Thanks for your time.