简体   繁体   English

ASP.NET MVC5 Select2无法正常工作

[英]ASP.NET MVC5 Select2 not working

This is my first message in this forum, thanks in advance for your help and sorry if I make a bigger mistake here. 这是我在该论坛上的第一条消息,在此先感谢您的帮助,如果在这里遇到更大的错误,对不起。

I'm trying to implement a select2 JQuery autocomplete combobox. 我正在尝试实现select2 JQuery自动完成组合框。

I copy some code and I can't see where is my mistake, but the edit text looks like a normal Edit text instead change as a Select2, the JavaScrypt is not working for some reason. 我复制了一些代码,但看不到我的错误在哪里,但是编辑文本看起来像是普通的编辑文本,而是更改为Select2,JavaScrypt出于某种原因无法正常工作。

Here is my code: 这是我的代码:

The references in my indexcshtml: 我的indexcshtml中的引用:

<link href="~/Content/css/select2.css" type="text/css" rel="stylesheet" />
<script src="~/Scripts/jquery-3.1.0.js"></script>
<script src="~/Scripts/select2.js"></script>
<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>

my textbox who should be converted in a Combobox like cyndarela ! 我的文本框,应该在像cyndarela这样的组合框中进行转换!

@using (Html.BeginForm())
{

    @Html.TextBoxFor(a => a.SupplierId, new { id = "supplier" })  @Html.ValidationMessageFor(a => a.SupplierId)
    <br />
    <button type="submit">Submit</button>
}

and at the end my JavaScrypt: 最后是我的JavaScrypt:

<script >



 $(document).ready(function () {

       var pageSize = 20;

       var optionListUrl = '@Url.Action("GetProducts", "Purchases")';

    //Method which is to be called for populating options in dropdown //dynamically

       $('#supplier').select2(

       {

           ajax: {

               delay: 150,

               url: optionListUrl,
               //url: '/Purchases/GetProducts',

               dataType: 'json',



               data: function (params) {

                   params.page = params.page || 1;

                   return {

                       searchTerm: params.term,

                       pageSize: pageSize,

                       pageNumber: params.page

                   };

               },

               processResults: function (data, params) {

                   params.page = params.page || 1;

                  return {

                       results: data.Results,

                       pagination: {

                           more: (params.page * pageSize) < data.Total

                       }

                   };

               }

           },

           placeholder: "-- Select --",

           minimumInputLength: 0,

           allowClear: true,

   });

});

</script>

is like #supplier is not working in the script 就像#supplier在脚本中不起作用

Thanks for your time guys ! 谢谢你们的时间!

I had the same issue. 我遇到过同样的问题。 The instruction 指令

@Scripts.Render("~/bundles/scripts")

In your layout view includes all your scripts and they sometimes have some instructions that dispose the .send2() 在布局视图中包括所有脚本,并且有时它们会包含一些处理.send2()的指令。

So you can delete that line and just import the scripts .js files that you need including select2. 因此,您可以删除该行,而只需导入所需的脚本.js文件(包括select2)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM