【问题标题】:Jquery select2 plugin with asp.net mvc4 and ajax带有 asp.net mvc4 和 ajax 的 Jquery select2 插件
【发布时间】:2015-04-30 10:39:52
【问题描述】:

我正在尝试使用控制器类返回的 JSON 将数据填充到 Select2 下拉列表中。但它不起作用。没有错误。这是代码

客户端

$("#products").select2({
        minimumInputLength: 2,
        ajax: {
            url: "Search",
            dataType: 'json',
            type: "POST",
            quietMillis: 50,
            data: function (term) {
                return {
                    "q": JSON.stringify(term),
                };
            },
            results: function (data) {
                return {
                    results: $.map(data, function (item) {
                        return {
                            text: item.text,
                            id: item.id
                        }
                    })
                };
            }
        }
    });

控制器动作

 [HttpPost]
    public JsonResult Search(string q)
    {
       //testing data
       return Json(new products() {id = "2", text = "biotouch"});      
    }

产品类别

public class products()
{
  public string id{get;set;}
  public string text{get;set;}
}

【问题讨论】:

    标签: ajax asp.net-mvc-4 jquery-select2


    【解决方案1】:

    当我改变它时它工作了

     results: function (data) {
    

    ProcessResults: function (data) {
    

    【讨论】:

      【解决方案2】:
      $("#products").select2({
                  minimumInputLength: 2,
                  ajax: {
                      url: "YourControllerName/Search",
                      dataType: 'json',
                      type: "POST",
                      quietMillis: 50,
                      data: function (term) {
                          return {
                              "q": JSON.stringify(term),
                          };
                      },
                      results: function (data) {
                          return {
                              results: $.map(data, function (item) {
                                  return {
                                      text: item.text,
                                      id: item.id
                                  }
                              })
                          };
                      }
                  }
              });
      

      我在 URL 中添加了控制器名称,您忘记在 url 中添加控制器名称。

      【讨论】:

        猜你喜欢
        • 2015-08-25
        • 1970-01-01
        • 2014-07-31
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多