jQuery getJSON, Autocomplete Uncaught TypeError: Cannot read property 'label' of undefined via MVC C#

jQuery getJSON, Autocomplete Uncaught TypeError: Cannot read property 'label' of undefined via MVC C#

大家好,我有个奇怪的问题:

我将使用 JSON 从我的 mvc 控制器推送一个列表到我在 js 上的 URL 我通过“$.getJSON”得到这个 json 并且我尝试将一个参数(输入字段的源)传递到控制器中,以使控制器上的过滤器正常工作。

遗憾的是我没有得到任何结果,几秒钟后控制台说我有一个 :

 Uncaught TypeError: Cannot read property 'label' of undefined at jquery-ui.min.js:9

所以这是我的控制器的代码:

public JsonResult GetAllImportantUsers(String cnFilter) {
            List<User> list = adController.GetUsersFromMultipleOUs(adPaths.InternalUsers,adPaths.TestUsers,adPaths.ExternalUsers,adPaths.ServiceUsers,adPaths.AdminUsers);

            if (cnFilter.Length >= 2)
            {
                Debug.WriteLine("CNFILTER Getting Value: " + cnFilter+ " Length: " + cnFilter.Length);
                return Json(list.Where(item => item.Cn.StartsWith(cnFilter, StringComparison.InvariantCultureIgnoreCase)), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return null;
            }

        }

这是js:

/**
 * 
 * @param {any} id Input ID where the autocomplete should start
 * @param {any} id2 second id for another input
 */
function userIDAutocomplete(id, id2) {

    var inputfield = $("#" + id);
    var secondInput = $("#" + id2);
    var tab = 9;
    var url = "/JSON/GetAllImportantUsers";

    $.getJSON(url,
        {
            cnFilter: inputfield.val()
        },

        function (internalusers) {
            cnFilter = inputfield.val();
            var allUserCN = [];
            for (i = 0; i < internalusers.length; i++) {

                //"push" all entries with cn in it in allUserCN
                allUserCN.push(internalusers[i]["cn"]);
            }

            $(inputfield).autocomplete({
                sortResults: true,
                autoFocus: true,
                source: function (request, response) {

                    // result will be sliced to the first 10 entries
                    var results = $.ui.autocomplete.filter(allUserCN, request.term);
                    response(results.slice(0, 10));
                }

            }).keyup(function (e) {
                if (e.keyCode === 13) {
                    inputfield.attr("disabled", true);
                    //overwrite another input 
                    secondInput.val(inputfield.val());
                    secondInput.attr("disabled", true);
                }
                }).keydown(function (e) {
                    if (e.keyCode === 9) {
                        inputfield.attr("disabled", true);
                        //overwrite another input 
                        secondInput.val(inputfield.val());
                        secondInput.attr("disabled", true);
                    }   
                });
        }
    );

这里是 html 中的实现:

<input type="text" class="form-control mb-2" placeholder="User-ID*" id="cnName" spellcheck="false" oninput="userIDAutocomplete('cnName','userIDAuthField');">

也许你们中有人知道我能做什么...

你必须 return List 在 json 而不是 IEnumerable:

var list = list.Where(item => item.Cn.StartsWith(cnFilter, StringComparison.InvariantCultureIgnoreCase).ToList();

return Json(list), JsonRequestBehavior.AllowGet);