【问题标题】:ASP.Net MVC Jquery Autocomplete - list not showingASP.Net MVC Jquery 自动完成 - 列表未显示
【发布时间】: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


    【解决方案1】:

    您可以在 GetJsonUsers 上设置断点并跟踪到错误。您还可以使用 f12 查看返回到浏览器的内容。

    【讨论】:

    • GetJsonUsers 函数有效。在我的浏览器中(使用 f12)我没有看到新元素/标签
    【解决方案2】:

    如下操作:

           $('#txtListUsers').autocomplete({
              source:function( request, response ) {
                  $.ajax({
                  url: "@Url.Action("YourActionName","YourControllerName")",
                  dataType: "json",
                  data: {},
                  success: function( data ) {
                     response( $.map( data, function( item ) {
                        return {
                           label: item.name,
                           value: item.name
                               }
                             }));
                  }
                 });
                 },
               minLength: 2
           });
    

    【讨论】:

    • 您是否调试过并查看您在成功调用 ajax 时收到的数据是什么?
    • 我已经更新了我的答案。立即关注网址。并设置 brealpoint 动作以及它是否被击中。
    • 是的,动作是调用并返回对象。
    • 好的,那现在有什么问题?
    • 我尝试使用 firebug 进行 dubug javascript 代码,“数据”似乎为空。我没有看到自动完成的元素列表
    猜你喜欢
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2018-09-21
    • 2020-05-29
    • 2012-03-10
    相关资源
    最近更新 更多