ASP.NET Core发布数组对象JSON

14

我想在我的视图中发布一个对象的数组到我的控制器,但是参数为空。我发现,就算是一个简单的对象,我也需要在我的控制器操作中使用 [FromBody]。

这是我的 JSON:

{
  "monJour": [
    {
      "openTime": "04:00",
      "closedTime": "21:30",
      "id": "0"
    },
    {
      "openTime": "08:00",
      "closedTime": "17:30",
      "id": "1"
    },
    {
      "openTime": "08:00",
      "closedTime": "17:30",
      "id": "2"
    },
    {
      "openTime": "08:00",
      "closedTime": "17:30",
      "id": "3"
    },
    {
      "openTime": "08:00",
      "closedTime": "17:30",
      "id": "4"
    },
    {
      "openTime": "08:00",
      "closedTime": "17:30",
      "id": "5"
    },
    {
      "openTime": "08:00",
      "closedTime": "17:30",
      "id": "6"
    }
  ]
}

这是我的Ajax请求:

function SendAllDay() {
    var mesJours = {};
    var monJour = [];
    mesJours.monJour = monJour;

    var senddata ='';

    $('div[id^="Conteneur"]').each(function () {
        var idDiv = $(this).attr("id");
        var jour = idDiv.substr(9, idDiv.length - 9);
        var opentps = $("#O" + jour).val();
        var closetps = $("#C" + jour).val();
        var monid = $("#id" + jour).val();

        monJour = {
            openTime: opentps,
            closedTime: closetps,
            id: monid
        }

        mesJours.monJour.push(monJour);
    });

    $.ajax({
        url: '@Url.Action("ReceiveAll")',
        dataType: 'json',
        type: 'POST',
        //data: JSON.stringify(monJours),
        data: JSON.stringify(mesJours),
        contentType:'application/json',
        success: function (response) {
            console.log('ok');
            //window.location.reload();
        },
        error: function (response) {
            console.log('error');
            //alert(response)
            //window.location.reload();
        }
    });
}

这是我的行动:

[HttpPost]
public void ReceiveAll([FromBody]ReceiveTime [] rt) { }

这是我的班级:

public class ReceiveTime
{
    public string openTime { get; set; }
    public string closedTime { get; set; }
    public string id { get; set; }
}

非常感谢任何帮助 :)

2个回答

23

不要使用ReceiveTime[] rt,根据您POST的相同结构对数据进行建模可能更好。例如,您可以创建一个类,看起来像这样:

public class MesJours
{
    public ReceiveTime[] MonJour { get; set; }
}

我不知道MesJours作为类名是否有意义(我不会说法语),但是这个想法仍然很清晰 - 你可以随意命名类名。

鉴于此,你可以像这样更新你的控制器:

[HttpPost]
public void ReceiveAll([FromBody] MesJours mesJours)
{
    // Access monJour via mesJours.
    var rt = mesJours.MonJour;
}

这将满足ASP.NET MVC模型绑定器,并提供您POST的数据。这还具有轻松适应任何您想要POST的其他属性的附加好处。


我不知道模型绑定器无法接受数组,但这解决了我的问题。 - TheNaturalTanuki
附加说明:不要忘记更改发送的数据以匹配新的预期数据模型。数组与匹配名称 - 在此示例中为“MonJour” - 作为请求对象的唯一属性。 - TheNaturalTanuki

0

1. 添加一个类,Mesjours:

public class MesJours
{
    public ReceiveTime[] MonJour { get; set; }
}   

2. 在控制器操作中将返回类型更改为 async Task(适用于 aspnetcore)

[HttpPost]
public async Task<IActionResult> ReceiveAll([FromBody]MesJours mesJourData)
{
    var rt = mesJourData.MonJour;
}

3.在视图中,确保发布的数据与控制器操作中的参数名称相同(在本例中为mesJourData)。注意ajax方法中包含数据参数的语法。

$.ajax({
        url: '@Url.Action("ReceiveAll")',
        dataType: 'json',
        type: 'POST',
        data:{mesJourData: mesJours} ,
        contentType:'application/json',
        success: function (response) {
            console.log('ok');
            //window.location.reload();
        },
        error: function (response) {
            console.log('error');
            //alert(response)
            //window.location.reload();
        }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接