简体   繁体   English

选择2; 如何在我从数据库加载一次的数组中加载

[英]Select2; how to load in an array that I load once from the database

I am loading an array with the results of the following Action: 我正在使用以下操作的结果加载数组:

public ActionResult GetJudges(string q)
    {
        var judges = new List<Judge>();
        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
        {
            using (var cmd = con.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "query";
                con.Open();
                using (var rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        var judge = default(Judge);
                        var id = Convert.ToInt32(rdr["ID"]);
                        var email = Convert.ToString(rdr["Email"]);
                        var name = Convert.ToString(rdr["Name"]);
                        var jurisdiction = Convert.ToString(rdr["Jurisdiction"]);
                        var code= Convert.ToString(rdr["code"]);

                        if (!judges.Select(x => x.Id).Contains(id))
                        {
                            judge = new Judge
                            {
                                Id = id,
                                Email = email,
                                Name = name,
                                Code= code
                            };
                            judges.Add(judge);
                        }
                        else
                        {
                            judge = judges.FirstOrDefault(x => x.id == id);
                        }

                        judge.Jurisdictions.Add(jurisdiction);
                    }
                }
                con.Close();
            }
        }
        return Json(judges, JsonRequestBehavior.AllowGet);
    }

I am trying to load this array into a Select2 element, and having no success. 我正在尝试将此数组加载到Select2元素中,但没有成功。 The select2 element is always empty. select2元素始终为空。 Here is the javascript I am using to load the select2: 这是我用来加载select2的javascript:

var judges = [];


    $(document).ready(function () {
        $.getJSON('/Home/GetJudges', function (result) {
            judges = result;
            $(".select2").select2({
                placeholder: "Search for a Judge",
                data: { results: result, id: "Id", text: "Text" }
            });
        });
    });

Can anyone tell me what I am missing here? 谁能告诉我我在这里想念的东西吗?

Look at this example on how to use select2. 看看这个关于如何使用选择2例。 You are passing the data incorrectly. 您传递的数据不正确。 Your result should contain an array of objects that have an id property and a text property, and you should be passing that to select2 as data directly, rather than wrapping it in another object. 您的result应包含具有id属性和text属性的对象数组,并且应将其直接传递给select2作为data ,而不是将其包装在另一个对象中。

//sample data
var result = [{id: 0, text: 'Judge 1'}, {id: 1, text: 'Judge 2'}];

$(".select2").select2({
    placeholder: "Search for a Judge",
    data: result
});

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

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