0

person! I really need help, I'm trying to do an Autocomplete with multiple inputs in ASP.NET Core, but at the moment I'm only able to return one value. When I enter the person's name I can bind the city, I would like to bind email, department

    [HttpPost]
    public JsonResult AutoCompletePeoples(string prefix)
    {
        var pessoas = (from pessoa in _context.Pessoas
                          where pessoa.NamePeople.StartsWith(prefix)
                          select new
                          {
                              label = pessoa.NamePeople,
                              val = pessoa.City

                          }).ToList();

        return Json(pessoas);
    }


 <script type="text/javascript">
    $(function () {
        $("#txtNomeVisitante").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '/Visitas/AutoCompletePeoples/',
                    data: { "prefix": request.term },
                    type: "POST",
                    success: function (data) {
                        response($.map(data, function (item) {
                            return item;
                        }))
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
            select: function (e, i) {
                $("#txtCity").val(i.item.val);


            },
            minLength: 1
        });
    });

</script>

2 Answers 2

0

According to your description, if you want to bind other property, you should also add the value when selecting from the backend and bind it inside the autocomplete select function.

More details, you could refer to below test codes:

View:

<input id="txtNomeVisitante" /> 
<hr/>
<input id="txtCity" />
<hr />
<input id="email" />
<hr />
<input id="department" />

Jquery:

    $(function () {
        $("#txtNomeVisitante").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '/Journey/AutoCompletePeoples/',
                    data: { "prefix": request.term },
                    type: "POST",
                    success: function (data) {
                        response($.map(data, function (item) {
                            return item;
                        }))
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
            select: function (e, i) {
                console.log(i.item.val)
                $("#txtCity").val(i.item.val);
                $("#email").val(i.item.email);
                $("#department").val(i.item.department);

            },
            minLength: 1
        });
    });

</script>

Backend:

    [HttpPost]
    public JsonResult AutoCompletePeoples(string prefix)
    {

        List<Pessoas> Pessoas = new List<Pessoas>() { new Pessoas { NamePeople="Brando", City= "City1", department= "department1", email= "email1" },
        new Pessoas { NamePeople="Lucy", City= "City2", department= "department2", email= "email2" },
        new Pessoas { NamePeople="Wendy", City= "City3", department= "department3", email= "email3" },

        };

        var pessoas = (from pessoa in Pessoas
                       where pessoa.NamePeople.StartsWith(prefix)
                       select new
                       {
                           label = pessoa.NamePeople,
                           val = pessoa.City,
                           department = pessoa.department,
                           email= pessoa.email

                       }).ToList();

        return Json(pessoas);
    }
}

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Brando Zhang,Thanks a lot for the help !!! But the problem continues, I can only return the "Celular" field that is in val] I would like it to return the other values
What's the Email and CPE value you have put to the client?
in javaScript $("#email").val(i.item.email); $("#cpf").val(i.item.cpf);
Brando Zhang,Thanks a lot for the help ..the full code is above
I want to know the backend class Visitante make up
|
0

Brando Zhang,Thanks a lot for the help !!! But the problem continues, I can only return the "Celular" field that is in val] I would like it to return the other values

[HttpPost] public JsonResult AutoCompletePeoples(string prefix) {

List<Visitante> visitantes = _context.Visitantes.ToList();


var pessoas = (from pessoa in _context.Visitantes
               where pessoa.NomeVisitante.StartsWith(prefix)
               select new
               {
                   label = pessoa.NomeVisitante,
                   val = pessoa.Celular,
                   CPF = pessoa.CPF,
                   email = pessoa.Email

               }).ToList();

return Json(pessoas);

}

<script type="text/javascript">
    $(function () {
        $("#txtNomeVisitante").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '/Home/AutoCompletePeoples/',
                    data: { "prefix": request.term },
                    type: "POST",
                    success: function (data) {
                        response($.map(data, function (item) {
                            return item;
                        }))
                    },
                    error: function (response) {
                        alert(response.responseText);
                    },
                    failure: function (response) {
                        alert(response.responseText);
                    }
                });
            },
            select: function (e, i) {
            console.log(i.item.val)           
            $("#celular").val(i.item.val);            
            $("#email").val(i.item.email);
            $("#cpf").val(i.item.cpf);
            },
            minLength: 1
        });
    });

</script>

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.