Unable to send array data using ajax with ASP.NET Core and Entity Framework Core

asp.net-core asp.net-core-2.0 asp.net-mvc entity-framework-core

Question

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 });
             }
         });
     });

     // alert(JSON.stringify(tests));
     $.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 method:

[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("Index");
    }

    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 tried the [FromBody] tag inside action params, but it does not work either!

Help me resolve this

1
0
4/10/2020 5:48:44 AM

Accepted Answer

you are sending a string with no names so the controller can not get the values.

change you code to

$.ajax({
type:"POST",
url:url,
data:test
...
});

the test should be an object not a string.

0
4/10/2020 3:51:52 AM

Popular Answer

You can pass the list of objects by :

$.ajax({
    type: "POST",
    url: "Reception/PatientTests/SavePendingTest",
    data: { pendingTestResult: tests },

    headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
    success: function (data) {
        alert(data);
    },
    error: function (e) {
        alert('Error' + JSON.stringify(e));
    }
});

pendingTestResult in data:{ pendingTestResult: tests } matches the parameter name on action and remove the contentType setting .



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow