我對着在自動填充文本框中聖維特硬編碼數據的問題,我的JSON「搜索」的方法不火我已經查了很多代碼落實到我的項目,但沒有得到任何成功了。我不知道問題在哪裏。好心幫我thankx提前如何實現自動完成文本框在MVC
Model:
public class Locations
{
public int Id { get; set; }
public string Name { get; set; }
}
Controller:
public JsonResult Search(string query)
{
List<Locations> locations = new List<Locations>()
{
new Locations() {Id = 1, Name = "London"},
new Locations() {Id = 2, Name = "Walles"},
new Locations() {Id = 3, Name = "Birmingham"},
new Locations() {Id = 4, Name = "Edinburgh"},
new Locations() {Id = 5, Name = "Glasgow"},
new Locations() {Id = 6, Name = "Liverpool"},
new Locations() {Id = 7, Name = "Bristol"},
new Locations() {Id = 8, Name = "Manchester"},
new Locations() {Id = 9, Name = "NewCastle"},
new Locations() {Id = 10, Name = "Leeds"},
new Locations() {Id = 11, Name = "Sheffield"},
new Locations() {Id = 12, Name = "Nottingham"},
new Locations() {Id = 13, Name = "Cardif"},
new Locations() {Id = 14, Name = "Cambridge"},
new Locations() {Id = 15, Name = "Bradford"},
new Locations() {Id = 16, Name = "Kingston Upon Hall"},
new Locations() {Id = 17, Name = "Norwich"},
new Locations() {Id = 18, Name = "Conventory"}
};
List<string> Loc;
Loc = locations.Where(x => x.Name.StartsWith(query.ToLower())).Select(x => x.Name).ToList();
return Json(Loc, JsonRequestBehavior.AllowGet);
}
View:
@model IEnumerable<SearchBox.Models.Locations>
@using SearchBox.Models
@{
ViewBag.Title = "Index";
}
<link href="~/Content/Autocomplete/jquery-ui.css" rel="stylesheet" />
<script src="~/Content/Autocomplete/jquery-ui.js"></script>
<link href="~/Content/Autocomplete/jquery-ui.theme.css" rel="stylesheet" />
<script type="text/javascript">
$("#tags").autocomplete({
source: '@Url.Action("Search")'
});
</script>
<input type="text" id="tags" />
您可能需要使用源分配AJAX調用:'源:功能(要求(URL)參數設置爲'url:'@ Url.Action(「Search」)'「來創建或者添加'query'字符串參數到''response'){$ .ajax({...})})' Url.Action'輔助方法。 –
@TetsuyaYamamoto試過這個,但我不知道問題出在哪裏,什麼都沒有發生:( –
即使是Json方法也不會調用哪裏列表綁定 –