【发布时间】:2013-09-07 22:30:32
【问题描述】:
我已在我的应用中实现了自动完成功能以检索用户列表,但当我搜索用户时该列表未显示。
在我的 _Layout.cshtml 中:
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
<script src="~/Scripts/jquery-2.0.3.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-ui-1.10.3.js" type="text/javascript"></script>
在我看来:
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$('#txtListUsers').autocomplete({
source: '@Url.Action("GetJsonUsers","GestioneLega")',
minLength: 2
});
});
})
</script>
...
<input type="text" id="txtListUsers" />
我的行动:
public JsonResult GetJsonUsers(string term)
{
var users = GestServices.GetUsersForAutocomplete(term);
return Json(users, JsonRequestBehavior.AllowGet);
}
获取数据:
public static object GetUsersForAutocomplete(string searchTerm)
{
object users = null;
using (var db = new FriendsContext())
{
users = from cust in db.Users.Where(c => c.UserName.StartsWith(searchTerm))
select cust.UserName;
}
return users;
}
GetJsonUsers 函数不起作用: 做更多的测试,我注意到在GetUsersForAutocomplete 函数中,变量“users”只填充在使用范围内。如果我立即控制超出使用范围的用户,请获取: 操作无法完成,因为 DbContext 已被释放
我通过这个讨论The operation cannot be completed because the DbContext has been disposed error解决了这个问题
【问题讨论】:
标签: jquery asp.net asp.net-mvc