1,標籤
<input type="text" data-autocomplete-source="@Url.Action("QuickSearch","Demo")" />javascript
2,須要引用樣式和腳本
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="Stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>css
3,腳本代碼
<script type="text/javascript">
$(function () {
$("input[data-autocomplete-source]").each(function () {
$(this).autocomplete({ source: $(this).attr("data-autocomplete-source") });
});java
});
</script>jquery
4,控制器代碼
public JsonResult QuickSearch(string term)
{
var s = db.Demos.Where(r => r.Name.Contains(term)).ToList().Select(a => new { value = a.Name });
//必須容許來自客戶端的GET請求。不過這樣會出現安全隱患
return Json(s, JsonRequestBehavior.AllowGet);
}安全