使用 jQuery 的 MVC2 Ajax 自动完成
我正在尝试在 MVC2 中实现 Ajax 自动完成,但没有成功。它没有进入自动完成功能。你能告诉我出了什么问题吗?这是我的脚本。
$('#Contact').change(function () {
debugger;
$('#Contact').autocomplete({
source: function (request, response) {
$.ajax({
url: "/ChapterRelationship/GetContacts",
data: {searchText: request.term, maxResults: 10},
type: "POST", // http method
datatype: "json",
success: function (msg) {
// ajax call has returned
var result = msg;
var a = [];
if (result !== null) {
for (var i = 0; i < result.length; i++) {
a.push({ label: result[i].prop1, id: result[i].prop2 });
}
} responseFn(a);
}
});
}
});
});
这是我的控制器。
[HttpPost]
public JsonResult GetContacts(string id)
{
// return Content("test");
return this.Json("test", JsonRequestBehavior.AllowGet);
}
谢谢
I am trying to implement Ajax autocomplete in MVC2 and having no success. It is not getting into the autocomplete. Can you tell me what is wrong? Here is my script.
$('#Contact').change(function () {
debugger;
$('#Contact').autocomplete({
source: function (request, response) {
$.ajax({
url: "/ChapterRelationship/GetContacts",
data: {searchText: request.term, maxResults: 10},
type: "POST", // http method
datatype: "json",
success: function (msg) {
// ajax call has returned
var result = msg;
var a = [];
if (result !== null) {
for (var i = 0; i < result.length; i++) {
a.push({ label: result[i].prop1, id: result[i].prop2 });
}
} responseFn(a);
}
});
}
});
});
Here is my controller.
[HttpPost]
public JsonResult GetContacts(string id)
{
// return Content("test");
return this.Json("test", JsonRequestBehavior.AllowGet);
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 MVC2 和 jQuery 自动完成的相当完整的示例:
http ://theycallmemrjames.blogspot.com/2010/03/jquery-autocomplete-with-aspnet-mvc.html
我不确定您是否提供了足够的代码来给出更完整的答案,尽管其他人可能会无需完整代码即可发现问题。
Here is a fairly complete sample of MVC2 and jQuery autocomplete:
http://theycallmemrjames.blogspot.com/2010/03/jquery-autocomplete-with-aspnet-mvc.html
I'm not sure you've provided enough code to give a more complete answer, although someone else may be able to spot the issue without complete code.