使用 MVC 的 jQuery UI 自动完成无法显示返回的项目
我已经为这个问题绞尽脑汁有一段时间了,我不确定我做错了什么。
我对我的一个控制器进行了一个非常简单的 JSON 调用,我想使用 jquery ui 自动完成功能返回客户端名称以及相关 ID 值。我可以在 Firebug 中看到结果,但自动完成功能始终返回空列表。
jquery 生成的列表中的项目数是正确的 - 例如,如果我返回 3 个结果,那么我会看到带有 3 个空白列表项目的建议菜单。问题似乎是 jquery 没有正确解析我的响应。
我没有像这里建议的其他一些问题那样在文本框中使用 jquery 验证。
如果这有影响的话,我会在本地运行它。
下面是我使用的 jquery/HTML:
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript"> google.load("jquery", "1.6.4")</script>
<script type="text/javascript"> google.load("jqueryui", "1.8.16")</script>
<script type="text/javascript">
$(document).ready(function () {
$("#clientEntered").autocomplete({
source: '@Url.Action("ClientAutoSuggest", "Clients")'
});
});
</script>
<input type="text" name="clientEntered" id="clientEntered" />
我的控制器看起来像这样:
public JsonResult ClientAutoSuggest(string term)
{
var filteredClients = (from c in clientService.GetClients()
where c.Name.ToLower().StartsWith(term.ToLower())
select new { ClientID = c.ClientID, Name = c.Name }).Take(10);
return Json(filteredClients, JsonRequestBehavior.AllowGet);
}
如上所述,我确实看到 Firebug 中传回的结果,但生成的列表始终为空。
示例 - 如果返回 2 个结果,我将在 Firebug 中看到 2 个结果:
[{"ClientID":1,"Name":"Client 1"},{"ClientID":2,"Name":"Client 2"}]
如果我查看生成的源,我会看到以下内容(正确的 2 项但空白):
<ul style="z-index: 1; top: -16px; left: 0px; display: block; width: 1864px; position: relative;" aria-activedescendant="ui-active-menuitem" role="listbox" class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all"><li></li><li></li></ul>
我尝试了不同的配置来解析响应,但我尝试的任何操作似乎都不起作用工作。任何建议或帮助都会很棒,谢谢!
编辑:由于下面凯文的回答,它得以正常工作。问题在于没有正确映射响应。下面的代码现在运行良好。
$("#clientEntered").autocomplete({
source: function(request, response) {
$.ajax({
url: '@Url.Action("ClientAutoSuggest", "Clients")',
data: request,
dataType: "json",
type: "POST",
success: function(data){
response($.map(data,function(item){
return { label: item.Name,value: item.Name, id: item.ClientId }
}));
}
});
},
});
I've been banging my head against this issue for a while now and I'm not sure what I'm doing wrong.
I have a very simple JSON call to one of my controllers that I want to return client names along with the relevant ID values using jquery ui autocomplete. I can see the results in Firebug but the autocomplete is always returning an empty list.
The number of items in the list generated by jquery is correct - for example, if I am returned 3 results then I see the suggest menu with 3 blank list items. The problem seems to be that the jquery does not parse my response correctly.
I am not using jquery validate on the textbox as some other questions on here have suggested.
I am running this locally if that makes a difference.
Below is the jquery/HTML I use:
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript"> google.load("jquery", "1.6.4")</script>
<script type="text/javascript"> google.load("jqueryui", "1.8.16")</script>
<script type="text/javascript">
$(document).ready(function () {
$("#clientEntered").autocomplete({
source: '@Url.Action("ClientAutoSuggest", "Clients")'
});
});
</script>
<input type="text" name="clientEntered" id="clientEntered" />
My controller looks something like this:
public JsonResult ClientAutoSuggest(string term)
{
var filteredClients = (from c in clientService.GetClients()
where c.Name.ToLower().StartsWith(term.ToLower())
select new { ClientID = c.ClientID, Name = c.Name }).Take(10);
return Json(filteredClients, JsonRequestBehavior.AllowGet);
}
As mentioned above, I do see results passed back in Firebug but the generated list is always empty.
Example - if 2 results are returned I will see the 2 results in Firebug:
[{"ClientID":1,"Name":"Client 1"},{"ClientID":2,"Name":"Client 2"}]
If I look at the generated source I see the following (correct 2 items but blank):
<ul style="z-index: 1; top: -16px; left: 0px; display: block; width: 1864px; position: relative;" aria-activedescendant="ui-active-menuitem" role="listbox" class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all"><li></li><li></li></ul>
I have tried different configurations for parsing the response but nothing I try seems to work. Any suggestions or help would be great, thanks!
EDIT: Got it working thanks to Kevin's answer below. The issue was down to not mapping the response correctly. The code below works great now.
$("#clientEntered").autocomplete({
source: function(request, response) {
$.ajax({
url: '@Url.Action("ClientAutoSuggest", "Clients")',
data: request,
dataType: "json",
type: "POST",
success: function(data){
response($.map(data,function(item){
return { label: item.Name,value: item.Name, id: item.ClientId }
}));
}
});
},
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到任何参考文献 2 实际自动完成的 JS?你可能在其他地方有它,但我只是想让你知道。
真正的问题可能是由您的“@Url.Action”引起的。它只会执行该操作并返回结果。 *没有到文本框的映射。
最佳实践只是进行简单的 Ajax 调用,并在收到响应时进行映射。
应该是这样的,只是写得很快,所以不要向我开枪,但你应该得到照片;)
I don't see any reference 2 the actually autocomplete JS? You probably have it somewhere else but I just wanted to let you know.
The real problem probably is caused by your '@Url.Action'. It will just execute that action and return the results. There is *no mapping to the textbox.
The best practice would just to make a simple Ajax call and do the mapping when you receive the response.
Should be something like this, just wrote it quick so don't shoot me but you should get the picture ;)