【问题标题】:PagedList MVC With JQuery UI Autocomplete Causing ErrorPagedList MVC 与 JQuery UI 自动完成导致错误
【发布时间】:2015-05-11 17:03:22
【问题描述】:

我正在尝试在文本框上使用 JQuery UI Autocomplete 来过滤分页列表并动态更新列表,因此它只显示以过滤器中的值开头的任何内容。但是,每当我开始在搜索框中输入内容时,都会出现以下错误:

语法错误:意外的令牌

这是我的控制器:

    [HttpGet]
    public ActionResult Index(string filter, int currentPage = 1)
    {
        List<string> allEmails = null;

        if (!string.IsNullOrEmpty(filter))
        {
            allEmails = dataContext.Emails
                .Where(x => x.email.StartsWith(filter))
                .Select(x => x.email)
                .ToList();
        }
        else
        {
            allEmails = dataContext.Emails
                .Select(x => x.email)
                .ToList();

        }
        PagedList<string> model = new PagedList<string>(allEmails, page, pageSize);
        ViewBag.Emails = allEmails.OrderBy(x => x).Skip((page - 1) * pageSize).Take(pageSize);
        ViewBag.Filter = filter;
        return View(model);
    }

这是我的观点:

 @model PagedList<string>
 @using PagedList
 @using PagedList.Mvc

<div id="paginatedDiv">
    <table id="pageinatedTable">
         <tr>
             <th>
                 Email
             </th>
         </tr>
         @foreach (var item in ViewBag.Emails)
         {
              <tr>
                 <td>
                     @item
                 </td>
             </tr>
         }
     </table>

     @Html.PagedListPager(Model,
        (page) => Url.Action("Index", "Home", new
        {
            page,
            pageSize = Model.PageSize,
            filter = ViewBag.Filter
        }),
        PagedListRenderOptions.ClassicPlusFirstAndLast)
 </div>
 @Html.TextBox("search")


 @section scripts
 {
     <script type="text/javascript">
         $(document).ready(function () {
              $('#search').autocomplete({
                     source: function(request, response)
                     {
                        $.ajax({
                            url: '@Url.Action("Index", "Home")',
                            dataType: "json",
                            contentType: 'application/json, charset=utf-8',
                            data: {
                                filter : $("#search").val(),
                                page : '@Model.PageNumber'
                            },

                            error: function (xhr, status, error) {
                                alert(error);
                            }

                        })},
                      minlength: 1
             });

         });
     </script>
  }

我在这里尝试做的可能吗?我是不是走错了路?如果您需要更多信息,请告诉我。

【问题讨论】:

  • 我认为 ajax 函数正在返回一个 HTML 错误,该错误试图解析为该函数。它会失败,因为第一个字符将是
  • 谢谢,成功了:)
  • 很高兴听到:)..将其作为答案..标记它已解决...

标签: ajax asp.net-mvc asp.net-mvc-5 jquery-ui-autocomplete pagedlist


【解决方案1】:

Ajax 函数返回一个 HTML 错误,该错误正试图解析为该函数。它将失败并出错,因为第一个字符将是 '

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 2011-04-02
    • 2011-05-31
    • 2011-02-03
    • 2014-07-20
    • 1970-01-01
    相关资源
    最近更新 更多