gpt4 book ai didi

javascript - MVC4 中的自动完成

转载 作者:行者123 更新时间:2023-11-30 18:21:29 25 4
gpt4 key购买 nike

在我的项目中使用自动完成时遇到问题。我正在使用 MVC4。我已经使用 Json 部分正确地遵循了所有内容。我不确定问题是出在 jQuery 上还是在我的 Controller 上。以下是代码

public ActionResult Index()
{
EmployeeContext db = new EmployeeContext();
return View(db.Employees);
}
[HttpPost]
public ActionResult Index(string Search_Data)
{
EmployeeContext db = new EmployeeContext();
List<Employee> employees;
if (string.IsNullOrEmpty(Search_Data))
{
employees = db.Employees.ToList();
}
else
{
employees = db.Employees
.Where(s => s.EmpName.StartsWith(Search_Data)).ToList();
}
return View(employees);
}
public JsonResult GetEmployees(string term)
{
EmployeeContext db = new EmployeeContext();
List<string> employees = db.Employees.Where(s => s.EmpName.StartsWith(term))
.Select(x => x.EmpName).ToList();
return Json(employees, JsonRequestBehavior.AllowGet);
}

我的 index.cshtml 中使用了以下脚本

    <link href="~/Content/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css"/>
<script src="~/Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-ui-1.10.4.custom.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#txtSearch").autocomplete({
source: '@Url.Action("GetEmployees","Employee")',
minLength: 1
});
});
</script>

问题是 GetEmployees 方法未命中,我可以通过输入字符串来搜索数据,但自动完成功能不起作用。

最佳答案

您似乎缺少一个重要的脚本文件:

Unobtrusive Ajax

我曾多次为这个问题所困扰。 jQuery 自动完成功能使用 jQuery ajax 函数,如果您包含 Unobtrusive Ajax 脚本,这仅适用于 MVC。

关于javascript - MVC4 中的自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com