我刚刚在视图中添加了自动完成功能。它不起作用,当我将焦点设置在文本框上并键入一个字母时,什么也没有出现。我不确定我在这里做错了什么,但这里是代码:
引用的 jQuery 文件:
<script src="@Url.Content("~/Scripts/jquery-1.8.3.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>
看法:
$(function () {
$("#rootcause").autocomplete({
source: function (request, response) {
alert("x");
$.ajax({
url: "/ServiceEntry/FindRootCause",
type: "POST",
dataType: "json",
data: {
searchText: request.searchText,
maxResult: 10
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item.FullName,
value: item.RootCauseName,
id: item.Id
}
}))
}
})
},
select: function (event, ui) {
alert(ui.item
? ("You picked '" + ui.item.label +
"' with an ID of " + ui.item.id)
: "Nothing selected, input was " + this.value
);
}
});
});
<div class="bodyContent">
<span class="leftContent">@Html.Label("Root Cause")</span>
<span class="rightContent">
<input id="rootcause" type="text" />
</span>
</div>
控制器动作:
[HttpPost]
public JsonResult FindRootCause(string searchText, int maxResult)
{
var result = RunLog.Domain.Lists.GlobalList.GetRootCause(searchText, maxResult);
return Json(result);
}
public static List<RunLog.Domain.Entities.RootCause> GetRootCause(string search, int maxResult)
{
//List<String> list = new List<String>();
EFDbContext db = new EFDbContext();
var list = (from rc in db.RootCause
where rc.RootCauseName.Contains(search)
orderby rc.RootCauseName
select rc).ToList();
return list.Take(maxResult).ToList();
}