2016-09-15 67 views
0

我有兩種模式。其中一個模型(我的ViewModel)綁定到表單中的文本框,另一個模型綁定到我用於自動完成文本框(V​​ieModel.Name)的實體(Employees)。我按照教程here,似乎無法得到它的工作。我也研究過Google。這可能是我對實體框架的新穎性,或者我使用ViewModel而不是綁定到與實體綁定的實體和模型的事實。Jquery自動完成不工作MVC與實體框架

最終,我試圖從Employees實體模型中的FirstName + LastName自動完成ViewModel中的名稱,但現在正在嘗試FirstName。從實體

型號:

public partial class Employee 
{ 
    public bool ActiveFlag { get; set; } 
    public int EmpNid { get; set; } 
    public string RecKey { get; set; } 
    public string FirstName { get; set; } 
    public string MiddleName { get; set; } 
    public string LastName { get; set; } 
    ... etc 

我的視圖模型不依賴於實體,而是用這種形式發佈:

public class SalesViewModel 
{ 
    [Display(Name = "Employee Name")] 
    [Required(ErrorMessage = "The employee name is required")] 
    public string Name { get; set; } 
    ... etc 

查看:

<div class="form-group"> 
    @Html.LabelFor(m => m.Name, new { @class = "col-md-2 control-label" }) 
    <div class="col-md-10"> 
     @Html.TextBoxFor(m => m.Name, 
     new 
     { 
      @class = "employees-autocomplete", 
      data_url = Url.Action("EmployeesAutocomplete", "Employee") 
     }) 
     @Html.ValidationMessageFor(m => m.Name, "", new { @class = "text-danger" }) 
    </div> 
</div> 

我的Jquery在的底部頁碼:

$(function() { 
    $('.employees-autocomplete').autocomplete({ 
     minLength: 0, 
     source: function (request, response) { 
      var url = $(this.element).data('url'); 

      $.getJSON(url, { term: request.term }, function (data) { 
       response(data); 
      }) 
     } 
    }); 
}) 

我JsonResult控制器:

public JsonResult EmployeesAutocomplete(string term) 
{ 
    Employee[] matching = string.IsNullOrWhiteSpace(term) ? 
     db.Employees.ToArray() : 
     db.Employees.Where(p => p.FirstName.ToUpper().StartsWith(term.ToUpper())).ToArray(); 

    return Json(matching.Select(m => new 
    { 
     id = m.EmpNid, 
     value = m.FirstName, 
     label = m.ToString() 
    }), JsonRequestBehavior.AllowGet); 
} 

我應該做的事情後或get請求?不知道我錯了哪裏。

回答

2

你的代碼看起來不錯。我看到的唯一問題是,您正在調用標籤值的Employee實體對象/記錄上的ToString()。由於這是要顯示給用戶的價值,你可能想使用你的名字或這裏的任何字符串屬性(除非你在Employee類覆蓋了一個ToString()方法返回一個字符串版本)

替換此

return Json(matching.Select(m => new 
{ 
    id = m.EmpNid, 
    value = m.FirstName, 
    label = m.ToString() 
}), JsonRequestBehavior.AllowGet); 

有了這個

return Json(matching.Select(m => new 
{ 
    id = m.EmpNid, 
    value = m.FirstName, 
    label = m.FirstName +' '+m.LastName 
}), JsonRequestBehavior.AllowGet); 

按照註釋

Uncaught ReferenceError: $ is not defined(anonymous function)

這通常發生在您試圖在頁面中定義jQuery之前嘗試使用jQuery。仔細檢查以下步驟

  1. 一切之前包括jQuery的
  2. 包括jQuery UI的jQuery庫
  3. 後才加載dependeny庫(jQuery的/ jQueryUI的)後,執行您的頁面級的腳本。您可以通過在腳本部分中調用頁面級腳本來完成此操作。

    @section scripts 
    { 
        $(function(){ 
    
        // Your code for initializing the auto complete goes here 
    
        }) 
    } 
    

    假設你的佈局有一個稱爲腳本的部分,它是在加載jQuery和jQuery UI庫之後定義的。

所以在佈局文件,它應該是這樣的

@RenderBody() 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> 
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/themes/smoothness/jquery-ui.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script> 
@RenderSection("scripts", required: false) 
+0

我在文本框中,仍然一無所獲開始打字。奇怪的是,我開始輸入時甚至沒有在開發者控制檯或網絡標籤中看到任何東西。 – justiceorjustus

+0

你的控制檯有任何錯誤嗎?檢查您的網絡標籤,看看它是否將您的通話作爲您的服務器。迴應是什麼? – Shyju

+0

它沒有撥打電話或返回錯誤。我只是在我的JsonResult EmployeesAutocomplete類中設置了一個斷點(我應該在提問之前完成此操作),並且在我開始輸入時甚至沒有進入該位置。 – justiceorjustus

0

對於MVC Artitecture您必須刪除已impended

@Scripts.Render("~/bundles/Jquery") 

而且

@Scripts.Render("~/bundles/Jquaryval") 

和所有.cshtml文件在結尾處對於views/Shared/_layout.cshtml在最後,並把我們的jQuery合適的文件在它的頭合適的.cshtmls文件。

戴上頭這些文件:

<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>