在自动完成中找不到结果时显示消息



我正在尝试使用以下代码在文本框中显示数据。当我输入a时,所有以a开头的记录都会出现在数据库的下拉列表中。但是,如果我输入数据库中不存在的另一个值,则无法显示"未找到记录"的消息。

$(function() {
$("#Symptoms").bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("ui-autocomplete").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: 1,
source: function(request, response) {
$.getJSON("/CheckUpMaster/GetSymptomsName", {
Prefix: extractLast(request.term)
}, response);
},
search: function() {
// custom minLength
var term = extractLast(this.value);
if (term.length < 1) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var usersIdVal = $("#Symptoms").val();
usersIdVal += ", " + ui.item.userId;
//$("#Symptoms").val(usersIdVal)
var terms = split(this.value);
// removethe current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
});
});
function split(val) {
return val.split(/,s*/);
}
function extractLast(term) {
return split(term).pop();
}

输入数据库中存在的值

数据库中不存在输入的值 给我解决方案。提前感谢!

我找到了问题的答案。 它可能会帮助他人。

public JsonResult GetSymptomsName(string Prefix)
{
context con = new context();
List<string> SymptomsList;
SymptomsList = con.SymptomsMaster.Where(k => k.Symptoms.ToLower().StartsWith(Prefix))
.Select(d => d.Symptoms).ToList();
if(SymptomsList == null || SymptomsList.Count == 0)
{
SymptomsList.Add("No Data Found");
}

return Json(SymptomsList, JsonRequestBehavior.AllowGet);
}

最新更新