【问题标题】:ASP.NET Core jquery Autocomplete returns blank lines in the listASP.NET Core jquery Autocomplete 返回列表中的空行
【发布时间】:2018-07-09 14:12:14
【问题描述】:

我正在使用 ASP.NET Core jquery Autocomplete 和 Boostrap 4 我已成功运行以下示例:https://jqueryui.com/autocomplete/

我现在希望使用来自控制器的数据来正确返回数据。 我得到的结果是空行。

这是我的 Razor 页面

<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div class="ui-widget">
    <label>Autocomplete Example: </label>
    <input id="LastName" name="LastName" type="text" />
</div>
<script>
    $("#LastName").autocomplete({
        source: '@Url.Action("GetName","Home")'
    });
</script>

这是我的控制器 [HttpGet] 公共 IActionResult GetName(字符串术语) {

        List<TransactionName> list = new List<TransactionName>()
        {

            new TransactionName {Id=1,LastName="Linda" },
            new TransactionName {Id=2,LastName="Donna" },
            new TransactionName {Id=3,LastName="Maryanne" },
            new TransactionName {Id=4,LastName="Deb" },
            new TransactionName {Id=5,LastName="Liz" },
            new TransactionName {Id=6,LastName="Bobby" },
            new TransactionName {Id=7,LastName="Beth" }

    };
        var result = (from N in list
                        where N.LastName.Contains(term)
                        select new {N.LastName });
        return Json(result);
    }

【问题讨论】:

    标签: jquery asp.net-core autocomplete


    【解决方案1】:

    我根据 jquery Autocomplete 文档更改了以下内容:

        var result = (from N in list
                        where N.LastName.Contains(term)
                        select new {value=N.LastName });
    

    这是取自他们网站的部分文档。 支持多种类型: 数组:数组可用于本地数据。有两种支持的格式: 字符串数组:[ "Choice1", "Choice2" ] 具有标签和值属性的对象数组: [ { label: "Choice1", value: "value1" }, ... ]

    【讨论】:

    • 如果你只是要接受自己的答案,那么用赏金作为诱饵是不礼貌的。
    猜你喜欢
    • 2012-09-05
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 2015-05-11
    • 2018-09-28
    • 1970-01-01
    相关资源
    最近更新 更多