我是一个初学者&我试图发展与ASP.Net MVC 5.自动完成搜索框,我使用Northwind数据库和Entity Framework 6ASP.Net MVC +自动完成搜索工作不
这里是我index.cshtml代码
@model IEnumerable<AutoComplete3.Models.Customers>
<link href="~/Content/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript" src="~/Scripts/jquery-1.9.1.js"></script>
<script type="text/javascript" src="~/Scripts/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
$("#txtSearch").autocomplete({
source: '@Url.Action("GetCustomers")'
});
});
</script>
@using (@Html.BeginForm())
{
<b>Name : </b>
@Html.TextBox("searchTerm", null, new { @id = "txtSearch" })
<input type="submit" value="Search" />
}
这里是我CustomerController类
public class CustomersController : Controller
{
northwindEntities db = new northwindEntities();
public ActionResult Index()
{
return View(db.Customers);
}
[HttpPost]
public ActionResult Index(string SearchTerm)
{
List<Customers> customers;
if (string.IsNullOrEmpty(SearchTerm))
{
customers = db.Customers.ToList();
}
else
{
customers = db.Customers.Where(c => c.CompanyName.StartsWith(SearchTerm)).ToList();
}
return View(customers);
}
public JsonResult GetCustomers(string term)
{
List<string> customers;
customers = db.Customers.Where(c => c.CompanyName.StartsWith(term)).Select(y => y.CompanyName).ToList();
return Json(customers,JsonRequestBehavior.AllowGet);
}
}
此代码的工作,当我搜索记录,通过输入关键字&点击提交按钮。但GetCustomer方法不能由jquery脚本调用。检查元素显示以下错误。
Uncaught TypeError: $(...).autocomplete is not a function
该文本框应建议公司名称的文本框本身。如何纠正这一点。
谢谢。
你加入JQ控制器正确使用吗?在浏览器中检查呈现的页面ViewSource。 – Raghuveer
按不加载的jQuery或它的插件错误消息正确 – Raghuveer
我试图通过以下[这](https://www.youtube.com/watch?v=quQgUNteWxY)视频做到这一点。 &这是如何我添加Jquery的我的视图 <链路HREF = “〜/内容/ jquery的-ui.css” 的rel = “样式表”/> <脚本类型= “文本/ JavaScript的” SRC =” 〜/ Scripts/jquery-1.9.1.js“> 它错了吗? – Chamith