我有兩種模式。其中一個模型(我的ViewModel)綁定到表單中的文本框,另一個模型綁定到我用於自動完成文本框(VieModel.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請求?不知道我錯了哪裏。
我在文本框中,仍然一無所獲開始打字。奇怪的是,我開始輸入時甚至沒有在開發者控制檯或網絡標籤中看到任何東西。 – justiceorjustus
你的控制檯有任何錯誤嗎?檢查您的網絡標籤,看看它是否將您的通話作爲您的服務器。迴應是什麼? – Shyju
它沒有撥打電話或返回錯誤。我只是在我的JsonResult EmployeesAutocomplete類中設置了一個斷點(我應該在提問之前完成此操作),並且在我開始輸入時甚至沒有進入該位置。 – justiceorjustus