Bootstrap Select2 ProcessResults Not Firing
本文关键字:Firing Not ProcessResults Select2 Bootstrap | 更新日期: 2023-09-27 18:27:54
我在使用Select2 v4填充json结果选项时遇到问题(https://select2.github.io/)。我一定是忽略了一些小东西,就是一根手指都放不上…
下拉:
<select id="e1" multiple="multiple"></select>
JavaScript:
<script type="text/javascript">
$("#e1").select2({
minimumInputLength: 1,
ajax: {
url: "/Empl.asmx/AccessRemoteData",
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term
};
},
// Why is this never called???
processResults: function (data) {
return { results: data };
}
},
minimumInputLength: 3
});
</script>
代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;
namespace WebUI
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Empl : System.Web.Services.WebService
{
public class Select2DTO
{
public string id { get; set; }
public string text { get; set; }
}
[WebMethod]
public string AccessRemoteData(string q)
{
Dictionary<string, string> dictCountry = new Dictionary<string, string>();
dictCountry.Add("1", "India");
dictCountry.Add("2", "Canada");
dictCountry.Add("3", "United States");
dictCountry.Add("4", "United Kingdom");
dictCountry.Add("5", "United Arab Emirates");
List<object> CountryList = new List<object>();
Select2DTO singleCountry;
var selectedCountryList = dictCountry.Where(m => m.Value.ToLower().Contains(q)).ToList();
foreach (var selectedCountry in selectedCountryList)
{
singleCountry = new Select2DTO();
singleCountry.id = selectedCountry.Key;
singleCountry.text = selectedCountry.Value;
CountryList.Add(singleCountry);
}
var json = JsonConvert.SerializeObject(CountryList, new JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None });
return json;
}
}
}
正在调用web服务。如果我去http://localhost:52657/Empl.asmx/AccessRemoteData?q=united我得到以下结果:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://tempuri.org/">
[{"id":"3","text":"United States"},{"id":"4","text":"United Kingdom"},{"id":"5","text":"United Arab Emirates"}]
</string>
如果我在客户端的"processResults"中放置断点,它永远不会被命中。今天早上我读了其他几篇帖子,似乎找不到这个问题。
有人帮忙吗?
实际上,整个问题的存在是因为您试图超越ASP.Net web服务。您只需要向客户端返回List<Select2DTO>
。一篇解释这一切(一个非常常见的陷阱)的好文章是这样的。
[WebMethod]
public List<object> AccessRemoteData(string q)
{
Dictionary<string, string> dictCountry = new Dictionary<string, string>();
dictCountry.Add("1", "India");
dictCountry.Add("2", "Canada");
dictCountry.Add("3", "United States");
dictCountry.Add("4", "United Kingdom");
dictCountry.Add("5", "United Arab Emirates");
List<object> CountryList = new List<object>();
Select2DTO singleCountry;
var selectedCountryList = dictCountry.Where(m => m.Value.ToLower().Contains(q)).ToList();
foreach (var selectedCountry in selectedCountryList)
{
singleCountry = new Select2DTO();
singleCountry.id = selectedCountry.Key;
singleCountry.text = selectedCountry.Value;
CountryList.Add(singleCountry);
}
return CountryList;
}