Marius Vasile

Marius Vasile

  • 590
  • 1.9k
  • 148.6k

asp.net core textbox autocomplete with jquery and json

May 16 2021 5:41 PM
I am trying to have a textbox with autocomplete using an example from here https://www.iaspnetcore.com/blog/blogpost/577e6e3784cd461ac830e4a9. However, I get no results. My code below
 
  1. <div class="col-md-1">  
  2.                 <select id="ConClass" class="form-control border-danger" asp-items="@Model.SelectControlClass">  
  3.                     <option value="">-- Select --</option>  
  4.                 </select>  
  5.                 <span asp-validation-for="RiskAssessmentSecondary.ConClass" class="text-danger"></span>  
  6.                 <input  asp-for="RiskAssessmentSecondary.ConClass" id="conClass" />  
  7.             </div>  
  8.             <div class="col-md-2">  
  9.                 <input id="txtControl" type="text" class="form-control" asp-for="RiskAssessmentSecondary.ConDetail" />  
  10.                 <span asp-validation-for="RiskAssessmentSecondary.ConDetail" class="text-danger"></span>  
  11.             </div>  
 
  1. $(function () {  
  2.             $("#ConClass").on("change"function () {  

  3.                 var data = $("#ConClass :selected").text();  
  4.                 $("#conClass").val(data);  
  5.                 $("#txtControl").autocomplete({  
  6.                     source: function (request, response) {  
  7.   
  8.                         $.getJSON(`?handler=AutoComplete`, request, function (data) {  
  9.                             response($.map(data, function (item) {  
  10.                                 return {  
  11.                                     value: item + ""  
  12.                                 }  
  13.                             }))  
  14.                         })  
  15.                     }  
  16.                 });  
  17.             });  
  18.         });  
  
  1. public JsonResult OnGetAutoComplete(string id)  
  2.         {  
  3.             var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);  
  4.             var orgid = _context.UsersData.Where(a => a.Id == userId).Select(a => a.OrgID).SingleOrDefault();  
  5.   
  6.             var conDetail = (from a in _context.HazardSources.Where(s => s.OrgID == orgid)  
  7.                              join b in _context.HazardClasses on a.IdHS equals b.IdHS into Temp1  
  8.                              from c in Temp1  
  9.                              join d in _context.HazardDetails on c.IdHC equals d.IdHC into Temp2  
  10.                              from e in Temp2  
  11.                              join f in _context.ControlClasses.Where(s => s.ConClass.ToLower().Contains(id.ToLower())) on e.IdH equals f.IdH into Temp3  
  12.                              from g in Temp3 select new  
  13.                              {  
  14.   
  15.                                  value = g.ConDetail  
  16.   
  17.                              }).Distinct().ToList();  
  18.   
  19.   
  20.             return new JsonResult(conDetail);  
  21.         }  
 
 

Answers (1)