javascript - 使用 typeahead 在 C# 中自动完成不起作用

标签 javascript c# model-view-controller autocomplete jquery-ui-autocomplete

我的JS函数

<script type="text/javascript">
        $(function () {
            $("#pName").autocomplete({
                source: '@Url.Action("GetExistingProducts")'
            });
        });
</script>

ID #pNameModal 中文本框的 ID。

我在 Controller 中的 GetExistingProducts 函数

public IEnumerable<string> GetExistingProducts()
{
    return _traceProjectService.GetAllProducts();
}

这会在我的服务中调用 GetAllProducts()

public IEnumerable<string> GetAllProducts()
{
      var productList = myContext.Projects.Select(x =>x.ProductName).ToList();
      return productList;    
}

问题:

当我开始在我的文本框中输入内容时,我的 JS 函数没有显示现有的产品。

引用资料:

  1. http://jqueryui.com/autocomplete/

如果有人能告诉我我做错了什么,我将不胜感激。谢谢。

最佳答案

试试这个:

<script src="~/Scripts/bootstrap3-typeahead.js"></script>

<script>

    $.ajax({
        url: '@Url.Action("GetExistingProducts", "Admin")',
        type: "GET"
    }).done(function(dt) {
        var res = dt.split(",");
        $(".pText").typeahead({
            source: res,
            showHintOnFocus: "all",
            fitToElement: true
        });
    });
    $.ajax({
        url: '@Url.Action("GetExistingVersions", "Admin")',
        type: "GET"
    }).done(function(dt) {
        var res = dt.split(",");
        $(".vText").typeahead({
            source: res,
            showHintOnFocus: "all",
            fitToElement: true
        });
    });


</script>

在 Controller 端

public string GetExistingProducts()
        {
            var x = yourservice.function().toList();
            string a = string.Empty;
            foreach (var item in x)
            {
                a += item + ",";
            }

            return a;


        }

关于javascript - 使用 typeahead 在 C# 中自动完成不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51148582/

相关文章:

javascript - 在特定选择上显示单选按钮

c# - 将 asp.net 页面转换为 Word 文档

c# - 区域共享布局_ViewStart.cthml

java - 如何从绑定(bind)结果验证失败返回错误响应?

java - 每 20 毫秒将值从 Web 服务器推送到客户端 (Java MVC)

javascript - 如何在 Angular 2 Material 表的搜索结果中加粗搜索模式

javascript - "Must use destructuring state assignment": How to destructure from object and place on property inside object literal

javascript - 如何使 onclick 函数随机?

c# - 如何使用 NetworkManager 为所有客户端同步场景加载

c# - 如何在 C# 中从 TKey 获取字典 TValue?