Ajax 我无法在mvc中映射自动完成文本框值
事实上,当我调试我的代码时,它正在执行json操作,但搜索值像\“\a\”这样传递,我不明白有什么问题,有人帮我吗 我的看法是:Ajax 我无法在mvc中映射自动完成文本框值,ajax,asp.net-mvc,model-view-controller,asp.net-ajax,Ajax,Asp.net Mvc,Model View Controller,Asp.net Ajax,事实上,当我调试我的代码时,它正在执行json操作,但搜索值像\“\a\”这样传递,我不明白有什么问题,有人帮我吗 我的看法是: @model IEnumerable<autoCompelte.Models.Student> <script type="text/javascript"> $(document).ready(function() { $("#search").autocomplete({ source: f
@model IEnumerable<autoCompelte.Models.Student>
<script type="text/javascript">
$(document).ready(function() {
$("#search").autocomplete({
source: function (request, response) {
$.ajax({
url: "/Home/AutoComplte",
data: {term :JSON.stringify( request.term)},
type: "POST",
datatype:"json",
success: function (data) {
response($.map(data, function (item) {
return {
label: item.Description,
value: item.Id
}
}))
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve states.');
}
})
}
})
})
</script>
....
<p><input type="text" id="search" name="search"></p>
<table>
<tr>
<th>@Html.DisplayNameFor(model => model.Name)</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>@Html.DisplayFor(modelItem => item.Name)</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
data:{term:request.term},
我删除了JSON.stringify,然后字符串值以正确的格式传递,但是该值没有绑定到texboxAll,您返回的是一个字符串数组-没有属性Description
和Id
,因此标签:item.Description,
等不起作用(但不确定您想要什么)?当在文本框中键入字母时,匹配名称的字母应该像下拉列表一样绑定到下面的文本框,那么如何将其绑定到文本框,先生?@stephenTry justlabel:item,value:item
(但正如我所指出的,我不确定您想要显示什么)
public ActionResult Index()
{
return View(db.Students.ToList());
}
public JsonResult AutoComplte( string term)
{
var result = from n in db.Students
where n.Name.ToLower().StartsWith(term.ToLower())
select n.Name;
return Json(result, JsonRequestBehavior.AllowGet);
}