Javascript MVC4中的自动完成

Javascript MVC4中的自动完成,javascript,c#,Javascript,C#,在我的项目中,对于如何使自动完成工作存在问题。我用的是MVC4。我已经使用Json部分正确地跟踪了所有内容。我不确定问题是在jQuery上还是在我的控制器上。 下面是代码 public ActionResult Index() { EmployeeContext db = new EmployeeContext(); return View(db.Employees); } [HttpPost] public ActionResul

在我的项目中,对于如何使自动完成工作存在问题。我用的是MVC4。我已经使用Json部分正确地跟踪了所有内容。我不确定问题是在jQuery上还是在我的控制器上。 下面是代码

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);
    }
public ActionResult Index()
{
EmployeeContext db=新的EmployeeContext();
返回视图(db.Employees);
}
[HttpPost]
公共操作结果索引(字符串搜索\u数据)
{
EmployeeContext db=新的EmployeeContext();
列出员工名单;
if(string.IsNullOrEmpty(搜索数据))
{
employees=db.employees.ToList();
}
其他的
{
雇员=db.雇员
.Where(s=>s.EmpName.StartsWith(Search_Data)).ToList();
}
返回视图(员工);
}
公共JsonResult GetEmployees(字符串术语)
{
EmployeeContext db=新的EmployeeContext();
列出employees=db.employees.Where(s=>s.EmpName.StartsWith(term))
.Select(x=>x.EmpName).ToList();
返回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>

$(函数(){
$(“#txtSearch”).autocomplete({
来源:'@Url.Action(“GetEmployees”、“Employees”),
最小长度:1
});
});

问题是GetEmployees方法没有命中,我可以通过输入字符串搜索数据,但autocomplete不起作用。

看起来您缺少一个重要的脚本文件:


我对这个问题已经迷恋了很多次。jQuery自动完成功能使用jQuery ajax功能,只有在包含不引人注目的ajax脚本的情况下,该功能才适用于MVC。

我解决了错误,并将脚本放在了另一个区域,因此

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/angular")
<script src="~/Scripts/MyApp.js"></script>
<script src="~/Scripts/jquery-ui-1.10.4.custom.min.js" type="text/javascript"></script>   
@RenderSection("scripts", required: false)
@Scripts.Render(“~/bundles/jquery”)
@Scripts.Render(“~/bundles/angular”)
@RenderSection(“脚本”,必需:false)

您在控制台中是否遇到错误?没有,我没有收到任何错误。您的操作是否被调用?我包含了不引人注目的Ajax脚本,但没有任何用处,autosearch仍然无法工作