[英]How to show autocomplete from more than one column?
這段代碼僅從一列返回自動完成功能,而我的邏輯是從兩列中獲取自動完成功能。 我想知道是什么問題。 請,如果有人知道答案,請幫助我。
謝謝!
public JsonResult SearchFromTable(string name)
{
AccountingDataLayer.AccountingSMSEntities context = new AccountingDataLayer.AccountingSMSEntities();
var search = (from s in context.Products
where s.Name == name || s.FullCode == name
select new{s.FullCode , s.Name , s.Description });
return Json(search, JsonRequestBehavior.AllowGet);
}
public JsonResult AutoCompleteProductName(string term , string t)
{
AccountingDataLayer.AccountingSMSEntities context = new AccountingDataLayer.AccountingSMSEntities();
var result = (from p in context.Products
where p.Name.ToLower().Contains(term.ToLower()) || p.FullCode.ToLower().Contains(t.ToLower())
select new { p.Name , p.FullCode }).Distinct();
return Json(result, JsonRequestBehavior.AllowGet);
}
$("#search").autocomplete({
source: function (request, response) {
$.ajax({
url: "/InVoiceStock/AutoCompleteProductName",
type: "POST",
dataType: "json",
data: { term: request.term , t: request.t },
success: function (data) {
response($.map(data, function (item , t) {
return { label: item.Name, value: item.Name, label: t.FullCode, value: t.FullCode };
}))
}
})
},
messages: {
noResults: "", results: ""
}
});
});`
您不必要地創建了一整套t
變量。 該請求對象將僅具有一個term
屬性,該屬性引用文本輸入中當前的值。 所以request.t
在這里將是未定義的
data: { term: request.term , t: request.t }
向前移動,在操作方法中, string t
將為null,並且不會在過濾中使用
var result = (from p in context.Products
where p.Name.ToLower().Contains(term.ToLower()) || p.FullCode.ToLower().Contains(t.ToLower())
select new { p.Name , p.FullCode }).Distinct();
要更正它,請在where子句中在兩個條件中都使用term
並刪除對t
不必要引用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.