Sk Jha

Sk Jha

  • NA
  • 113
  • 43.4k

unable to send array data using ajax with asp.net core

Apr 10 2020 12:44 AM
I am trying to send array to the controller but it's blank in the controller parameter.
 
ajax function is
  1. $('#pending').click(function () {    
  2.             SaveTestResult("/Reception/PatientTests/SavePendingTest");    
  3.         });    
  4.         function SaveTestResult(url) {    
  5.             var pid = $('.patientId').attr('id');    
  6.             var tid = "";    
  7.             var tval = "";    
  8.             var tpid = "";    
  9.             var tests = [];    
  10.             $("table > tbody > tr").each(function () {    
  11.     
  12.                 testId = $(this).find('.tid').val();    
  13.     
  14.                 if (typeof (testId) != "undefined") {    
  15.                     tid = testId;    
  16.                 }    
  17.     
  18.                 var rowText = ""    
  19.     
  20.                 $(this).find('td').each(function () {    
  21.     
  22.                     tpid = $(this).find('.tpId').val();    
  23.                     tval = $(this).find('.result').val();    
  24.                     if (typeof (tpid) != "undefined") {    
  25.                         tests.push({ PatientId: pid, TestId: tid, TestParameterId: tpid, TestValue: tval });    
  26.                     }    
  27.                 });    
  28.     
  29.             });    
  30.             //alert(JSON.stringify(tests));    
  31.             $.ajax({    
  32.                 type: "POST",    
  33.                 url: url,    
  34.                 data: JSON.stringify(tests),    
  35.                 contentType: "application/json",    
  36.                 headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },    
  37.                 success: function (data) {    
  38.     
  39.                     alert(data);    
  40.                 },    
  41.                 error: function (e) {    
  42.                     alert('Error' + JSON.stringify(e));    
  43.                 }    
  44.             });    
  45.         }    
this is the controller
  1. [HttpPost]    
  2. [Route("Reception/PatientTests/SavePendingTest")]    
  3. public async Task<IActionResult> SavePendingTest(List<PendingTestResult> pendingTestResult)    
  4. {    
  5.     if (ModelState.IsValid)    
  6.     {    
  7.         foreach (PendingTestResult ptr in pendingTestResult)    
  8.         {    
  9.             _db.Add(ptr);    
  10.             await _db.SaveChangesAsync();    
  11.         }    
  12.   
  13.        // return new JsonResult("Index");    
  14.     }    
  15.   
  16.     return new JsonResult(pendingTestResult); ;    
  17. }   
But when run the code I see data array filled but inside of the SavePendingTest action, pendingTestResult is empty and not filled! I also try [FromBody] tag inside action params but it does not work too! help me to resolve this

Answers (2)