MVC ASP自动完成

时间:2017-08-26 05:39:37

标签: javascript jquery asp.net-mvc autocomplete

我知道这个问题上有一些帖子,但是我找不到任何选择。我疯了。 我是MVC和JavaScript的新手。我在控制台中收到以下错误:

  

FamilyStudentJS.js:20 Uncaught TypeError:$(...)。autocomplete不是函数           在HTMLDocument。 (FamilyStudentJS.js:20)           在火上(jquery-1.12.4.js:3232)           at Object.fireWith [as resolveWith](jquery-1.12.4.js:3362)           在Function.ready(jquery-1.12.4.js:3582)           在HTMLDocument.completed(jquery-1.12.4.js:3617)

我尝试过很多参考文献。不过,我的C#功能还没有启动。当我手动运行它时,它工作正常。

我的HTML代码:

<div class="form-group">
    @Html.LabelFor(model => model.FamilyNum, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10" id="fs">
            <label> <input type="checkbox" id="FamilyCheckBox"> Family attached? </label>
        <div id="familysection" style="display:none" >
            <span>
                <input type="text" id="FamilyIDTextBox" />
            </span>
        </div>
     @Html.ValidationMessageFor(model => model.FamilyNum, "", new { @class = "text-danger" })
    </div>
</div> 

JavaScript的:

$(document).ready(function () {
    $("#FamilyCheckBox").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "/Students/GetSearchValue",
                type: "POST", dataType: "json",
                data: { term: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.startfrom };
                    }));
                }
            });
        },
        messages: { noResults: "", results: "" }
    });
})

C#:

public JsonResult GetSearchValue(string search)
{
    var temp = new FamilyViewModel();

    EzappContext db = new EzappContext();
    List<Family> allsearch = db.Families.Where(x => x.FamilyName.Contains(search)).Select(x => new Family
    {
        FamilyName = x.FamilyName,
        FamilyNum = x.FamilyNum
    }).ToList();

    return new JsonResult { Data = allsearch, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

0 个答案:

没有答案