我第一次尝试为自动完成输入类型实现 JSON。
@{
ViewBag.Title = "Index";
}
<script type="text/javascript">
function searchFailed(){
$("#searchresults").html("Sorry, there was a problem with the search.");
}
$("input[data-autocomplete-source]").each(function () {
var target = $(this);
target.autocomplete({ source: target.attr("data-autocomplete-source") });
});
</script>
<h2>Index</h2>
@using (Ajax.BeginForm("QuickSearch", "Search", new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "GET", OnFailure = "searchFailed", LoadingElementId = "ajax-loader", UpdateTargetId = "searchresults", }))
{
<input type="text" name="q" data-autocomplete-source="@Url.Action("QuickSearch", "Search")" />
}
但它抱怨 data-autocomplete-source 不是一个有效的属性。它进入快速搜索,但我没有看到自动完成结果。