我的JS函数
<script type="text/javascript">
$(function () {
$("#pName").autocomplete({
source: '@Url.Action("GetExistingProducts")'
});
});
</script>
ID #pName
是 Modal 中文本框的 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 函数没有显示现有的产品。
引用资料:
如果有人能告诉我我做错了什么,我将不胜感激。谢谢。
最佳答案
试试这个:
<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/