自动填充无法正常工作mvc 5

时间:2017-04-29 08:25:13

标签: javascript asp.net-mvc jquery-ui-autocomplete

我正在使用自动填充选项来搜索名称以

开头的父母
 <script src="~/assets/lib/jquery/jquery.min.js" type="text/javascript"></script>
<script src="~/assets/lib/jquery-ui/jquery-ui.min.js" type="text/javascript"></script>
<link href="~/assets/lib/jquery-ui/jquery-ui.css" rel="stylesheet" type="text/css">
<script src="~/assets/lib/jquery.nanoscroller/javascripts/jquery.nanoscroller.js" type="text/javascript"></script>
    <link href="~/assets/lib/jquery.nanoscroller/css/nanoscroller.css" rel="stylesheet" type="text/css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>



<script type="text/javascript">
    $(function () {
        debugger;
            $("#Parent_FullName").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '@Url.Action("AutoComplete")',
                        data: "{ 'prefix': '" + request.term + "'}",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        success: function (data) {

                            response($.map(data, function (item) {
                                console.log(item);
                                return item;

                            }))
                        },
                        error: function (response) {
                            alert(response.responseText);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        }
                    });
                },
                select: function (e, i) {
                    $("#hfCustomer").val(i.item.val);
                    console.log(item.label);
                },
                minLength: 1
            });
        });
</script>

和控制器

 public JsonResult AutoComplete(string prefix)
   {
      var Parent = (from a in db.tblParents
                             where a.FirstName.StartsWith(prefix)
                             select new
                             {
                                 label = a.FirstName,
                                 val = a.ParentId
                             }).ToList();

            return Json(Parent);
        }

json返回值进入java脚本我可以在控制台上看到它但仍然没有自动完成,console.log(item)给出console/output不知道是什么问题?

我的观点是

<div class="header">


 <h4> PARENT DETAILS </h4>
                </div>
                <div class="content">
                    <div class="form-group">
                        @Html.LabelFor(model => model.Parent.FullName, " First Name ", htmlAttributes: new { @class = "control-label col-md-2 required" })
                        <div class="col-md-5">



                            <input type="text" id="Parent_FullName" name="Parent_FullName" />
                        </div>
                    </div>
                </div>
            </div>

0 个答案:

没有答案