为什么 ajax 自动完成中的项目未定义?
我不明白 .net core 中的自动完成出了什么问题
<p class="par-style">
<input type="text" name="SearchString" placeholder="search..." class="col-sm-5 search-style" id="searchInput"/>
<input type="submit" value="Search" class="search-button"/>
<input type="submit" value="Clear" class="search-button" onclick="clearData();"/>
<script>function clearData(){ document.getElementById("searchInput") = '';}
$(document).ready(function () {
$("#searchInput").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Jewlery/AutoComplete",
type: "POST",
dataType: "json",
data: { Prefix: request.term },
success: function (data) {
response($.map(data, function (item) {
return item;
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
}
});
});
</script>
</p>
然后,我有控制器
[HttpPost]
public JsonResult AutoComplete(string prefix)
{
return Json( _jewleryService.AutoComplete(prefix));
}
这个函数返回一个类型的对象
public class Jewlery
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string ImageUrl { get; set; }
public string ImageThumbnailUrl { get; set; }
public bool IsOnSale { get; set; }
public bool IsInStock { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
我也尝试返回一个对象 label:item.Name, value:item.Id,但它仍然不起作用。知道为什么吗?
I don't understand what goes wrong in autocomplete in .net core
<p class="par-style">
<input type="text" name="SearchString" placeholder="search..." class="col-sm-5 search-style" id="searchInput"/>
<input type="submit" value="Search" class="search-button"/>
<input type="submit" value="Clear" class="search-button" onclick="clearData();"/>
<script>function clearData(){ document.getElementById("searchInput") = '';}
$(document).ready(function () {
$("#searchInput").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Jewlery/AutoComplete",
type: "POST",
dataType: "json",
data: { Prefix: request.term },
success: function (data) {
response($.map(data, function (item) {
return item;
}))
},
error: function (response) {
alert(response.responseText);
},
failure: function (response) {
alert(response.responseText);
}
});
}
});
});
</script>
</p>
Then, I have the controller
[HttpPost]
public JsonResult AutoComplete(string prefix)
{
return Json( _jewleryService.AutoComplete(prefix));
}
This function returns an object of type
public class Jewlery
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string ImageUrl { get; set; }
public string ImageThumbnailUrl { get; set; }
public bool IsOnSale { get; set; }
public bool IsInStock { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
}
I have also tried to return an object label:item.Name, value:item.Id, but it still does not work. Any idea why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码没有任何问题。问题是我试图访问 item.Name 而不是 item.name
There was nothing wrong with the code. The problem was I was trying to access item.Name instead of item.name
根据您的代码,您正在传递一个对象,但根据 官方文档, autoComplete返回的数据类型是lable和Value的格式,所以会报错。如果成功,您可以将格式更改为自动完成支持的格式,如下所示。
下面是一个工作demo,大家可以参考一下。
customer.cs:
HomeController.cs:
索引视图:
结果:
According to your code, you are passing an object, but according to the official documentation, the data type returned by autoComplete is the format of lable and Value, so you will report an error. You can change the format to the format supported by autoComplete like below in your success.
A work demo as below ,you can refer to it.
customer.cs:
HomeController.cs:
Index view:
result: