【问题标题】: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#
【发布时间】:2018-07-27 10:22:25
【问题描述】:

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

我将使用 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');">

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

【问题讨论】:

    标签: javascript json model-view-controller jquery-ui-autocomplete


    【解决方案1】:

    您必须在 json 中返回 List 而不是 IEnumerable

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

    【讨论】:

    • 但我得到了一个“用户”对象列表,然后我将列表推送到 json 中。当我输入带有参数的 url 时,普通过滤器本身工作得很好......但是 js 的另一边似乎不起作用
    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2013-01-15
    • 2019-02-12
    • 2021-09-20
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多