这是我在.cshtml中的代码:
@using (Ajax.BeginForm("Search","Home",
new AjaxOptions
{
UpdateTargetId = "SearchResults",
HttpMethod ="GET",
InsertionMode = InsertionMode.Replace
}
))
{
<input type="text" id="q" data-autocomplete="@Url.Action("QuickSearch", "Home")" />
<input type="submit" value="Search" />
}
<table id="SearchResults">
</table>
在.js文件中:
$(document).ready(function () {
$(":input[data-autocomplete]").each(function () {
$(this).autocomplete({ source: $(this).attr("data-autocomplete") });
});
});
在HomeController.cs中,我的快速搜索是:
public ActionResult QuickSearch(string term)
{
var _restaurant = context.Books.Where(r => r.BookTitle.Contains(term) )
.Take(10)
.Select(r => new
{
label = r.BookTitle
});
return Json(_restaurant, JsonRequestBehavior.AllowGet);
}
我还添加了jquery-1.5.1.min.js、jquery-ui.css、jquery-ui.js、jquery.unobtrusive-ajax.min.js、modernizr-1.7.min.js。
我的搜索按钮工作得很好,但是当我在QuickSearch方法上放置断点并调试它时,即使未调用该方法,自动完成功能也根本无法工作,并且从不调用并执行该方法。 但是这个:
<a href="@Url.Action("QuickSearch", "Home")">url</a>
依然运行得很好。
在我的其他项目中,也一切正常。
感谢你的帮助!
I resolved this problem. I had a partial view in my page that had these at the top:
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
so i deleted this script block from partial view and it works now.