此代码仅返回一列中的自动完成,我的逻辑是从两列中获取它。我想知道问题是什么。如果有人知道答案,请帮帮我 谢谢!
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: ""
}
});
});`
答案 0 :(得分:0)
您已经不必要地创建了一整套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
的不必要引用