【问题标题】:Why the select2 is not populating the control with the latest data?为什么 select2 没有用最新数据填充控件?
【发布时间】:2019-10-24 12:46:37
【问题描述】:

我正在使用它进行渐进式搜索。

从技术上讲,它可以工作,但控件没有被返回的数据填充。

 $("#selUser").select2({
            ajax: {
                url: "/M01EngineeringData/GetFunctionalLocations",
                type: "GET",
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    return {
                        search: params.term // search term
                    };
                },
                processResults: function (response) {
                   // alert(response);
                    return {
                        results: response

                    };
                },
                cache: true
            }
        });

HTML:

   <div class="col-md-4 col-sm-12">
                            <div class="col-md-5">
                                <span class="Label_small_bold">Progressive Search :</span>
                            </div>
                            <div class="col-md-7">
                                <select id='selUser' style='width: 200px;'>
                                    <option value='0'>- Search user -</option>
                                </select>
                            </div> 
                        </div>

控制器:

 public JsonResult GetFunctionalLocations(string search)
        {
            try
            {
                return Json(enggDataService.GetFunctionalLocations(search) , JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {

                throw;
            }
        }

这个控制器正在以正确的格式返回数据。

["Test Func Location","Test Func Location","Test Func Location"]

但是 selUser 控件没有填充数据,为什么?

【问题讨论】:

    标签: jquery asp.net json ajax asp.net-mvc


    【解决方案1】:

    您需要通过循环响应来返回类似的内容 { id: "值属性", text: "标签属性", 元素:HTMLOptionElement } 参考 - https://select2.org/options

    【讨论】:

      猜你喜欢
      • 2019-08-07
      • 2017-01-18
      • 2018-02-01
      • 2020-04-28
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      相关资源
      最近更新 更多