【问题标题】:Unable to send array data using ajax with ASP.NET Core and Entity Framework Core无法通过 ASP.NET Core 和 Entity Framework Core 使用 ajax 发送数组数据
【发布时间】:2020-07-22 19:16:10
【问题描述】:

我正在尝试将数组发送到控制器,但控制器参数中为空白。

Ajax 函数是:

$('#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));
                }
    });
}

这是控制器方法:

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

但当运行代码时,我看到数据数组已填充,但在 SavePendingTest 操作内部,pendingTestResult 为空且未填充!我还尝试了动作参数中的[FromBody] 标签,但它也不起作用!

帮我解决这个问题

【问题讨论】:

    标签: asp.net-mvc asp.net-core entity-framework-core asp.net-core-2.0


    【解决方案1】:

    您正在发送一个没有名称的字符串,因此控制器无法获取值。

    把你的代码改成

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

    test 应该是 object 而不是 string

    【讨论】:

    • 帮助我进一步澄清这一点,测试应该是一个对象而不是一个字符串。
    • 如果您发布一个字符串,那么它将是一个无键参数。您必须使用 Request.Query 获取所有字符串
    【解决方案2】:

    您可以通过以下方式传递对象列表:

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

    data:{ pendingTestResult: tests } 中的pendingTestResult 与操作中的参数名称匹配并删除contentType 设置。

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 2020-06-16
      • 1970-01-01
      • 2021-04-21
      • 2019-02-23
      • 1970-01-01
      • 2020-02-13
      • 2020-08-05
      • 1970-01-01
      相关资源
      最近更新 更多