MVC3 jQuery Autocomplete is not displaying the results

The following jQuery autocomplete code is not displaying the results in MVC3. When I debug the code, I can see it is calling the QuickSearchByLastName correctly. Can someone tell me if my code is incorrect? (I also tried with jquery-1.6.2.min.js with no luck) Thank you! Index.cshtml: @using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "GET", InsertionMode = InsertionMode.Replace, UpdateTargetId = "results" } )) { }
---------------------------------------------------------------- Search Controller: public ActionResult QuickSearchByLastName(string term) { using (var context = new CSCContext()) { var searchResults = context.Students .Where(s => s.LastName.Contains(term) && s.IsActive == true) .Take(10) .Select(s => new { label = s.LastName }); return Json(searchResults, JsonRequestBehavior.AllowGet); } } -------------------- _Layout.cshtml: @Content.Script("jquery-1.4.4.min.js", Url) @Content.Script("jquery.unobtrusive-ajax.min.js", Url) @Content.Script("jquery-ui.min.js", Url) @Content.Script("jquery.validate.min.js", Url) @Content.Script("jquery.validate.unobtrusive.min.js", Url) @Content.Script("CSC.js", Url) @RenderSection("scripts", false) ---------------------- CSC.js $(document).ready(function () { $(":input[data-autocomplete]").each(function () { $(this).autocomplete({ source: $(this).attr("data-autocomplete") } ); }); }); The following code fixed the issue: public ActionResult QuickSearchByLastName(string term) { var context = new CSCContext(); try { var searchResults = context.Students .Where(s => s.LastName.Contains(term) && s.IsActive == true) .Take(10) .Select(s => new { label = s.LastName }); return Json(searchResults.ToList(), JsonRequestBehavior.AllowGet); } finally { context.Dispose(); } }
You can indeed use json for the autocomplete plugin

以上就是MVC3 jQuery Autocomplete is not displaying the results的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏