rk1*_*962 1 asp.net-mvc jquery asp.net-mvc-3
以下jQuery自动完成代码未在MVC3中显示结果.当我调试代码时,我可以看到它正确调用QuickSearchByLastName.有人能告诉我我的代码是否不正确?(我也试过jquery-1.6.2.min.js但没有运气)谢谢!
Index.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);
}
}
Run Code Online (Sandbox Code Playgroud)
_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)
Run Code Online (Sandbox Code Playgroud)
CSC.js
$(document).ready(function ()
{
$(":input[data-autocomplete]").each(function ()
{
$(this).autocomplete({
source: $(this).attr("data-autocomplete")
}
);
});
});
Run Code Online (Sandbox Code Playgroud)
以下代码修复了该问题:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我试过复制你的场景无济于事,因为它总是对我有用.这就是我做的.
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);
}
}
Run Code Online (Sandbox Code Playgroud)Index.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")" />
}
Run Code Online (Sandbox Code Playgroud)我使用过jquery-1.5.1.min.js,jquery-ui-1.8.11.min.js默认情况下捆绑了ASP.NET MVC 3 RTM.我也尝试将其放入a Ajax.BeginForm并导入默认的不显眼的脚本,它仍然适用于我.