I am trying to send array to the controller but it's blank in the controller parameter.
ajax function is
- $('#pending').click(function () {
- SaveTestResult("/Reception/PatientTests/SavePendingTest");
- });
- function SaveTestResult(url) {
- var pid = $('.patientId').attr('id');
- var tid = "";
- var tval = "";
- var tpid = "";
- var tests = [];
- $("table > tbody > tr").each(function () {
-
- testId = $(this).find('.tid').val();
-
- if (typeof (testId) != "undefined") {
- tid = testId;
- }
-
- var rowText = ""
-
- $(this).find('td').each(function () {
-
- tpid = $(this).find('.tpId').val();
- tval = $(this).find('.result').val();
- if (typeof (tpid) != "undefined") {
- tests.push({ PatientId: pid, TestId: tid, TestParameterId: tpid, TestValue: tval });
- }
- });
-
- });
-
- $.ajax({
- type: "POST",
- url: url,
- data: JSON.stringify(tests),
- contentType: "application/json",
- headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
- success: function (data) {
-
- alert(data);
- },
- error: function (e) {
- alert('Error' + JSON.stringify(e));
- }
- });
- }
this is the controller
- [HttpPost]
- [Route("Reception/PatientTests/SavePendingTest")]
- public async Task<IActionResult> SavePendingTest(List<PendingTestResult> pendingTestResult)
- {
- if (ModelState.IsValid)
- {
- foreach (PendingTestResult ptr in pendingTestResult)
- {
- _db.Add(ptr);
- await _db.SaveChangesAsync();
- }
-
-
- }
-
- return new JsonResult(pendingTestResult); ;
- }
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