2016-05-13 63 views
0

我是一个初学者&我试图发展与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 

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

谢谢。

+0

你加入JQ控制器正确使用吗?在浏览器中检查呈现的页面ViewSource。 – Raghuveer

+0

按不加载的jQuery或它的插件错误消息正确 – Raghuveer

+0

我试图通过以下[这](https://www.youtube.com/watch?v=quQgUNteWxY)视频做到这一点。 &这是如何我添加Jquery的我的视图 <链路HREF = “〜/内容/ jquery的-ui.css” 的rel = “样式表”/> <脚本类型= “文本/ JavaScript的” SRC =” 〜/ Scripts/jquery-1.9.1.js“> 它错了吗? – Chamith

回答

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); 
    } 
+0

请添加注释,在页面底部....为什么这个答案是跌投票,所以我可以解释更多.. –

+0

相同的输出。 我改变了如下。 (文档).ready(函数(){(“#txtSearch”)。自动完成({'url'.Action(“GetCustomers”,“客户“)” }); }); Chamith

+0

OK请稍候,我会更新我的答案,并给您适当的解决方案。 –