Javascript MVC4-带ActionResult的引导Typeahead源代码
我正在使用MVC4和Entity Framework开发一个intranet web应用程序。在我的一个观点中,我必须实现自动完成功能。要做到这一点,我正在使用引导Typeahead。我试图通过我的动作(所以是我的函数)来输入元素,但它似乎不起作用 下面是我的操作结果,它返回一个Json:Javascript MVC4-带ActionResult的引导Typeahead源代码,javascript,jquery,asp.net-mvc,twitter-bootstrap,typeahead.js,Javascript,Jquery,Asp.net Mvc,Twitter Bootstrap,Typeahead.js,我正在使用MVC4和Entity Framework开发一个intranet web应用程序。在我的一个观点中,我必须实现自动完成功能。要做到这一点,我正在使用引导Typeahead。我试图通过我的动作(所以是我的函数)来输入元素,但它似乎不起作用 下面是我的操作结果,它返回一个Json: public ActionResult AutoComplete(string term) { var result = db.Persons.Where(p => p.FirstName.To
public ActionResult AutoComplete(string term)
{
var result = db.Persons.Where(p => p.FirstName.ToLower().Contains(term.ToLower()) || p.LastName.ToLower().Contains(term.ToLower())).ToList().Select(p => p.FullName).ToList();
return Json(result, JsonRequestBehavior.AllowGet);
}
我的视图和脚本:
@model IEnumerable<BuSIMaterial.Models.Person>
@{
ViewBag.Title = "Index";
}
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />
<h2>Index</h2>
<input type="text" class="typeahead" data-provide="typeahead">
@section Scripts
{
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/themes/base/css")
$(".typeahead").typeahead({
source: function (query, process) {
var persons = [];
map = {};
// This is going to make an HTTP post request to the controller
return $.post('/Person/AutoComplete', { query: query }, function (data) {
// Loop through and push to the array
$.each(data, function (i, person) {
map[person.Name] = person;
map[person.F]
persons.push(country.Name);
});
// Process the details
process(countries);
});
},
updater: function (item) {
var selectedShortCode = map[item].ShortCode;
// Set the text to our selected id
$("#details").text("Selected : " + selectedShortCode);
return item;
}
});
</script>
}
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@节脚本
{
@Scripts.Render(“~/bundles/jqueryval”)
@Scripts.Render(“~/bundles/jqueryui”)
@style.Render(“~/Content/themes/base/css”)
$(“.typeahead”).typeahead({
来源:功能(查询、流程){
var人员=[];
map={};
//这将向控制器发出HTTP post请求
返回$.post('/Person/AutoComplete',{query:query},函数(数据){
//循环并推送到阵列
$。每个(数据、功能(i、个人){
map[person.Name]=个人;
地图[人.F]
persons.push(国家名称);
});
//处理细节
进程(国家);
});
},
更新程序:函数(项){
var selectedShortCode=map[item].ShortCode;
//将文本设置为所选id
$(“#详细信息”).text(“选定:”+selectedShortCode);
退货项目;
}
});
}
在母版页中,我调用引导jquery文件。你知道发生了什么吗?你能告诉我们什么不起作用吗?您是在AJAX请求中遇到500错误还是404错误?Javascript错误??另外,当您通过AJAX执行post请求时,您可能希望使用
[HttpPost]
属性来修饰控制器操作。此外,您的
没有开始标记。您需要在$(document.ready(function(){})
中包装好您的预输入代码。此外,在您的post请求中,您正在发送一个名为查询的参数/Person/AutoComplete
。控制器操作正在查找名为term
的参数。这段代码有很多错误。