2016-05-13 65 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請稍候,我會更新我的答案,並給您適當的解決方案。 –