【发布时间】:2017-02-27 19:27:11
【问题描述】:
我目前正在尝试使用类似于此处所示的 Jquery-UI 的自动完成功能:Autocomplete dropdown in MVC5? ...我现在使用 jquery 自动完成帮助器查看了许多帖子,但无法找到我的问题。我的问题是 ajax 调用发出并成功命中 GetSchools 操作,该操作将返回正确且预期的数据,但随后没有任何反应。没有显示自动完成选项,也没有出现控制台错误。我的 js 中的 success 函数被 data 中的适当数量的项目击中,但之后没有任何反应。感谢所有帮助。
我的控制器操作:
public ActionResult ManualVerifications()
{
var vm = new ManualVerificationsViewModel();
return View(vm);
}
[HttpPost]
public ActionResult ManualVerifications(ManualVerificationsViewModel vm)
{
return View();
}
public JsonResult GetSchools(string term = "")
{
var schoolList = _schoolRepo.GetAll()
.Where(c => c.Name.ToLower().Contains(term.ToLower()))
.Select(x => new { label = x.Name, value = x.Id })
.Distinct().ToList();
return Json(schoolList, JsonRequestBehavior.AllowGet);
}
我的观点(ManualVerifications.cshtml):
@model ProEdVerificationPortal.Data.ViewModels.Verification.ManualVerificationsViewModel
@Html.LabelFor(model => model.SelectedSchoolName, new { @class = "control-label" })
@Html.TextBoxFor(model => model.SelectedSchoolName, new { @class = "form-control" })
@Html.HiddenFor(model => model.SchoolId)
以及视图中渲染的 JS:
$(document).ready(function () {
$("#SelectedSchoolName").autocomplete({
source: function (request, response) {
$.ajax({
url: '/verification/getschools',
type: "GET",
datatype: "json",
data: {
term: request.term
},
success: function (data) {
console.log(data);
response(data);
}
})
},
select: function (event, ui) {
$("#SelectedSchoolName").val(ui.item.label);
$("#SchoolId").val(ui.item.value);
return false;
},
focus: function (event, ui) {
$("#SelectedSchoolName").val(ui.item.label);
return false;
}
});
});
还有我非常简单的视图模型:
public class ManualVerificationsViewModel
{
public string SelectedSchoolName { get; set; }
public int SchoolId { get; set; }
}
从我的控制器返回的数据示例如下:
[ { label: "SeedSchool", value: 1 }
【问题讨论】:
-
发布了答案,但没有看到返回到 AJAX 的数据示例,很难知道发生了什么。还要检查控制台中的任何错误。请更新您的问题。
-
@Twisty 问题已更新——同样的行为仍在发生
-
也更新了我的答案。
-
@Twisty 我已经更新了我的 JS 以完全匹配你的(除了你的有一个我修复的小语法错误)并发布了记录的 console.log(data) 的图片
标签: jquery asp.net-mvc jquery-ui autocomplete