ASP.Net MVC +自动完成搜索无法正常工作

时间:2016-05-13 09:23:28

标签: c# jquery asp.net asp.net-mvc

我是初学者&我正在尝试使用ASP.Net MVC 5开发自动完成搜索框。我使用Northwind数据库和实体框架6。

这是我的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);
    }
}

当我搜索记录时,输入关键字&amp;点击提交按钮。但是jquery脚本无法调用GetCustomer方法。 Inspect Elements显示以下错误。

Uncaught TypeError: $(...).autocomplete is not a function

文本框应该为文本框本身建议公司名称。如何纠正这一点。

感谢。

1 个答案:

答案 0 :(得分:0)

的Javascript

    $(document).ready(function () {
        $("#txtSearch").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Action("GetCustomers","Home")',
                    type: "POST",
                    dataType: "json",
                    data: { searchTerm: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.CompanyName, value: item.CompanyName };
                        }))

                    }
                })
            },
            messages: {
                noResults: "", results: ""
            },
        });
    })

查看

@using (@Html.BeginForm())
{
    <b>Name : </b>
    @Html.TextBox("searchTerm", null, new { @id = "txtSearch" })
    <input type="submit" value="Search" />
}

控制器

请使用[HttpPost]

更新您的控制器
    [HttpPost]
    public JsonResult GetCustomers(string searchTerm)
    {
    List<string> customers;
    customers = db.Customers.Where(c => c.CompanyName.StartsWith(term)).Select(y => y.CompanyName).ToList();
    return Json(customers,JsonRequestBehavior.AllowGet);
  }