我无法在mvc中映射自动完成文本框值

时间:2016-03-10 09:17:06

标签: 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: 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>

这是我的控制器

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);
}

0 个答案:

没有答案