jquery - MVC3 jQuery 自动完成不显示结果

标签 jquery asp.net-mvc asp.net-mvc-3

以下 jQuery 自动完成代码未在 MVC3 中显示结果。当我调试代码时,我可以看到它正在正确调用 QuickSearchByLastName。有人可以告诉我我的代码是否不正确吗? (我也尝试过 jquery-1.6.2.min.js 但没有成功)谢谢!

索引.cshtml:

@using (Ajax.BeginForm(new AjaxOptions
{
    HttpMethod = "GET",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "results"
}
))
{               
    <input type="text" name="q" data-autocomplete="@Url.Action("QuickSearchByLastName","Search")"  />                 
}
<div id="results" >
</div>    


----------------------------------------------------------------
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")
                              }
                            );
    });

});

以下代码解决了该问题:

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();
            }                       
        }

最佳答案

我尝试复制你的场景但没有成功,因为它总是对我有用。这就是我所做的。

  1. 使用 Internet 模板创建新的 ASP.NET MVC 3 项目
  2. HomeController:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    
        public ActionResult QuickSearchByLastName(string term)
        {
            var results = Enumerable
                .Range(1, 5)
                .Select(x => new { 
                    id = x, 
                    label = "label " + x, 
                    value = "value " + x 
                });
            return Json(results, JsonRequestBehavior.AllowGet);
        }
    }
    
  3. 索引.cshtml

    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css" type="text/css" media="all" /> 
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
    
    <script type="text/javascript">
        $(function () {
            $(':input[data-autocomplete]').each(function () {
                $(this).autocomplete({
                    source: $(this).attr('data-autocomplete')
                });
            });
        });
    </script>
    
    @using (Html.BeginForm())
    {               
        <input type="text" name="q" data-autocomplete="@Url.Action("QuickSearchByLastName", "Home")" />
    }
    

我使用了 jquery-1.5.1.min.jsjquery-ui-1.8.11.min.js,它们默认与 ASP.NET MVC 捆绑在一起3 实时传输时间。我还尝试将其放入 Ajax.BeginForm 中,并导入默认的不引人注目的脚本,它仍然对我有用。

关于jquery - MVC3 jQuery 自动完成不显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6721088/

相关文章:

asp.net-mvc-3 - 获取 EditorTemplate 中 HTML 字段的有效 ID

jQuery 处理程序错误不是一个函数

jquery - nivo slider 标题中的 z-index 问题

c# - 如何从 Controller 触发弹出确认

c# JSON 序列化使用值而不是属性名称

asp.net-mvc - HtmlHelper 扩展方法的 HTML 编码

c# - Razor:自定义 BeginForm()-like Razor 一次性 block 在某些情况下不起作用

javascript - 为什么这个 Fancybox 的高度和宽度不起作用?

javascript - PreventDefault() 不适用于图像链接

asp.net-mvc - 使用 ASP.NET MVC,有没有办法捕获所有 POST 请求,而不管主机的 URL 是什么?