Marius Vasile

Marius Vasile

  • 552
  • 1.9k
  • 145.8k

asp.net core getting data from json and jquerry

Apr 14 2021 3:45 PM
I have two select and I want to populate the second but I don't know what am I doing wrong
  1. <div class="col-md-10 mb-2">    
  2.     <select id="IRSelect" class="form-control ml-1">    
  3.         <option value="">--Select Filtering Condition--</option>    
  4.         <option value="1">Issuer</option>    
  5.         <option value="2">Receiver</option>    
  6.     </select>    
  7. </div>    
  8. <div class="col-md-10">    
  9.     <select id="IRDetail" class="form-control ml-1" asp-items="@(new SelectList(string.Empty, "Issuer", "Issuer"))">    
  10.         <option value="">--Select Name--</option>    
  11.     </select>    
  12. </div>   
and json
  1. public async Task OnGetIRDetails1Async()    
  2. {    
  3.     var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);    
  4.     var orgid = await _context.UsersData.Where(s => s.Id == userId).Select(s => s.OrgID).FirstOrDefaultAsync();    
  5.     var todayYear = DateTime.Today.Year;    
  6.     var valIR = await _context.PTWContents.Where(s => s.OrgID == orgid && s.StartDate.Year == todayYear).Select(s =>    
  7.                                 new SelectListItem    
  8.                                 {    
  9.                                     Value = s.Issuer,    
  10.                                     Text = s.Issuer    
  11.                                 }).ToListAsync();    
  12.         
  13.     return new JsonResult(new SelectList(valIR , "Issuer""Issuer"));    
  14. }    
  15. public async Task OnGetIRDetails2Async()    
  16. {    
  17.     var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);    
  18.     var orgid = await _context.UsersData.Where(s => s.Id == userId).Select(s => s.OrgID).FirstOrDefaultAsync();    
  19.     var todayYear = DateTime.Today.Year;    
  20.     var valIR = await _context.PTWContents.Where(s => s.OrgID == orgid && s.StartDate.Year == todayYear).Select(s =>    
  21.                                 new SelectListItem    
  22.                                 {    
  23.                                     Value = s.Receiver,    
  24.                                     Text = s.Receiver    
  25.                                 }).ToListAsync();    
  26.         
  27.     return new JsonResult(new SelectList(valIR , "Receiver""Receiver"));    
  28. }  
and jquery
  1. $(function () {    
  2.     $("#IRSelect").on("change"function () {    
  3.         var categoryId = $(this).val();    
  4.   
  5.         if (categoryId = 1) {    
  6.             $("#IRDetail").empty();    
  7.             $("#IRDetail").append("<option value=''>--Select Name--</option>");    
  8.             $.getJSON(`?handler=IRDetails1`, (data) => {    
  9.                 $.each(data, function (i, item) {    
  10.                     $("#IRDetail").append(`<option value="${item.value}"> ${item.text}</option>`);    
  11.                 });    
  12.             });    
  13.         }    
  14.         if (categoryId = 2) {    
  15.             $("#IRDetail").empty();    
  16.             $("#IRDetail").append("<option value=''>--Select Name--</option>");    
  17.             $.getJSON(`?handler=IRDetails2`, (data) => {    
  18.                 $.each(data, function (i, item) {    
  19.                     $("#IRDetail").append(`<option value="${item.value}"> ${item.text}</option>`);    
  20.                 });    
  21.             });    
  22.         }    
  23.             
  24.     });    
  25. });

Answers (3)