JQuery 自动完成结果不出现
我正在 ASP.net MVC 2 中创建一个自动完成字段。问题是输入字段只是旋转,并且没有自动完成数据或“无结果”消息出现。
我有一个搜索控制器,它使用 LIKE 语句从数据库中检索姓氏。该控制器是从下面的 JavaScript 函数调用的:
$('#author').autocomplete('~/edit/search.mvc', {
delay: 200,
minChars: 2
});
这一切都工作正常。我可以调试到搜索控制器,它会传递搜索文本 (q) 并一直获取结果到 return 语句。控制器如下。
//Method to return author searches
public JsonResult Search(string q)
{
var locateFacultyDto = new LocateFacultyMemberDto() { SearchText = q };
var result = _facultyMemberModel.LocateFacilityMembersByLastNameLike(locateFacultyDto, 10);
var lastNames = new List<string>();
foreach (var facultyMember in result.FacultyMembers)
{
lastNames.Add(facultyMember.LastName);
}
return Json(lastNames, JsonRequestBehavior.AllowGet);
}
但随后在 HTML (Spark) 页面中没有出现任何结果。我有一个非常简单的文本输入:
<input id="author" type="text" />
有什么想法吗?我无法调试超出搜索控制器末尾的任何内容,但到目前为止一切似乎都很好。
I am creating an autocomplete field in ASP.net MVC 2. The problem is the input field just spins and no autocomplete data or "no results" messages appear.
I have a Search controller that retrieves LastNames from a database using a LIKE statement. This controller is called from the JavaScript function below:
$('#author').autocomplete('~/edit/search.mvc', {
delay: 200,
minChars: 2
});
This all works fine. I can debug into the Search controller and it is passing search text (q) and fetching results all the way through to the return statement. The controller is below.
//Method to return author searches
public JsonResult Search(string q)
{
var locateFacultyDto = new LocateFacultyMemberDto() { SearchText = q };
var result = _facultyMemberModel.LocateFacilityMembersByLastNameLike(locateFacultyDto, 10);
var lastNames = new List<string>();
foreach (var facultyMember in result.FacultyMembers)
{
lastNames.Add(facultyMember.LastName);
}
return Json(lastNames, JsonRequestBehavior.AllowGet);
}
But then in the HTML (Spark) page no results appear. I have a really simple Text input:
<input id="author" type="text" />
Any ideas? I can't debug any further than the end of the Search controller, but everything seems fine up to that point.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议在 Firefox 中使用 firebug 看看返回的实际响应是什么。
I would suggest using firebug in firefox look at what the actual response coming back is.
好的,由于以下帖子和上面的评论,我明白了:
http://blogs.msdn.com/b/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery .aspx
发布生成的代码,以防对其他人有帮助。
Javascript:这会调用“编辑”控制器中的“搜索”方法(详细信息如下)。添加“Parse”函数是关键,我在上面提供的链接中对此进行了解释:
完整的 Javascript 函数:
ASP.net MVC 2 控制器:
中提琴!自动完成功能会在搜索结果中列出用户名,选择名称后会将 ID 插入字段中,这很简单:
谢谢大家!
OK, I figured it out thanks to the following post and the comments above:
http://blogs.msdn.com/b/joecar/archive/2009/01/08/autocomplete-with-asp-net-mvc-and-jquery.aspx
Posting the resulting code in case it helps someone else.
Javascript: This calls the 'Search' method in the 'Edit' controller (details below). Adding the "Parse" function was the key, which is explained in the link I provided above:
The complete Javascript function:
The ASP.net MVC 2 Controller:
Viola! Autocomplete lists user names in the search results, and upon selecting a name inserts the ID into the field, which is simply:
Thanks everyone!