Textbox problem for autocomplete

Asked

Viewed 48 times

0

I can not enter the method that makes the query to return the nomes and the id´s of the person.

View:

<script type="text/javascript">
    $(document).ready(function () { 
    });
    $("#AutoComplete").autocomplete({
        source: function (request, response) {
            var pessoa = new Array();
            $.ajax({
                async: false,
                cache: false,
                type: "POST",
                url: "@(Url.Action("GetAutoComplete", "Catequizando"))",
                data: { "term": request.term },
                success: function (data) {
                    for (var i = 0; i < data.length ; i++) {
                        pessoa[i] = { label: data[i].nome, Id: data[i].id };
                    }
                }
            });
            response(pessoa);
        }
    });
</script>

@using (Html.BeginForm("CriarCatequizando", "Catequizando", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-group">
        @Html.Label("Introduza o nome do Pai:", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-3">

            @Html.TextBox("AutoComplete", null, new { autofocus = "", @class = "form-control" })
        </div>
    </div>
}

Controller:

[HttpPost]
public ActionResult GetAutoComplete(string term)
{
    // se o termo a pesquisar for nulo...
    if (string.IsNullOrWhiteSpace(term))
    {
        term = "";
    }
    // procura as pessoas com nomes começados pelo valor especificado em 'term'
    // a lista está limitada a 10 pessoas
    var pessoas = (from pessoa in db.Pessoa
                   where pessoa.Nome.StartsWith(term)
                   orderby pessoa.Nome ascending
                   select new
                   {
                       id = pessoa.PessoaID,
                       nome = pessoa.Nome
                   }).Take(10);

    // devolve a lista de pessoas
    return Json(pessoas, JsonRequestBehavior.AllowGet);
}
  • What is this autocomplete called? What autocomplete component are you using?

  • By mistake, I forgot to put the script. It was edited.

  • 1

    Do you get an error in the browser console (F12)? Remove the HttpPost and manual access its action, to see if it is returning the JSON correctly.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.