Mouhssine tahri

Mouhssine tahri

  • NA
  • 201
  • 9.9k

using autocomplete in mvc application

Oct 22 2020 8:49 AM
I want to use autocomplete input in my application WEB:
 
in my view I use this code :
  1. @using (Html.BeginForm(nullnull, FormMethod.Post))  
  2. {  
  3. <input type="text" id="txtCustomer" name="CustomerName" />  
  4. <input type="hidden" id="hfCustomer" name="CustomerId" />  
  5. }  
my code javascript:
  1. $("#txtCustomer").autocomplete({  
  2. source: function (request, response) {  
  3. $.ajax({  
  4. url: '/Service/AutoComplete/',  
  5. data: { "prefix": request.term },  
  6. dataType: "json",  
  7. type: "POST",  
  8. contentType: "application/json; charset=utf-8",  
  9. success: function (data) {  
  10. response($.map(data, function (item) {  
  11. //debugger;  
  12. console.log(item);  
  13. return item;  
  14. }))  
  15. },  
  16. error: function (response) {  
  17. alert(response.responseText);  
  18. },  
  19. failure: function (response) {  
  20. alert(response.responseText);  
  21. }  
  22. });  
  23. },  
  24. select: function (event, ui) {  
  25. $("#hfCustomer").val(ui.item.value);  
  26. $("#hfCustomer").val(ui.item.id);  
  27. },  
  28. minLength: 1  
  29. });  
the controller nammed ServiceController
  1. [HttpPost]  
  2. public JsonResult AutoComplete(string prefix)  
  3. {  
  4. var db = new GestionCourrierEntities();  
  5. var services = (from customer in db.Service  
  6. where customer.DesignationService.StartsWith(prefix)  
  7. select new  
  8. {  
  9. label = customer.DesignationService,  
  10. val = customer.CodeService  
  11. }).ToList();  
  12. return Json(services);  
  13. }  
my data
 
 
 
when I execute my application
 
I fill in input text : Dir
 
comman console.log(item); give me
{label: "Direction v", val: 1}
{label: "Direction A", val: 2}
 
that good
 
but this result don't appear in my view
 
thanks for help

Answers (4)