【发布时间】:2017-09-18 00:31:18
【问题描述】:
我在使用硬编码数据自动完成文本框时遇到问题,我的 json“搜索”方法没有触发我一直在搜索很多代码,将其实现到我的项目中,但尚未取得任何成功。我不知道问题出在哪里。请帮助我提前谢谢
型号:
public class Locations
{
public int Id { get; set; }
public string Name { get; set; }
}
控制器:
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);
}
查看:
@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 调用:
source: function (request, response) { $.ajax({ ... }) })并将 URL 参数设置为url: '@Url.Action("Search")'这样做,或者在Url.Action辅助方法中添加query字符串参数。跨度> -
@TetsuyaYamamoto 试过这个,但我不知道问题出在哪里,什么也没发生 :(
-
即使是Json方法也不调用list绑定的地方
标签: c# jquery asp.net-mvc jquery-ui