ASP.NET核心发布数组对象JSON

时间:2017-12-09 18:25:48

标签: c# asp.net-core asp.net-core-mvc model-binding

我试图在我的视图中将一个Object数组发布到我的控制器但是params为null我看到只需一个简单的对象我需要将[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; }
}

感谢任何帮助:)

3 个答案:

答案 0 :(得分:8)

不是使用ReceiveTime[] rt,而是根据POST的相同结构对数据进行建模可能更好。例如,您可以创建一个如下所示的类:

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

我不知道MesJours作为一个班级名称是否有意义(我不会讲法语),但这个想法仍然很明确 - 你可以随意命名这个班级。< / p>

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

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

这将满足ASP.NET MVC Model Binder,并且应该为您提供已发布的数据。这还有一个额外的好处,可以轻松容纳您可能想要POST的任何其他属性。

答案 1 :(得分:1)

ajax js请求中的名称需要与后端控制器中参数的名称一致。

尝试按如下方式更新控制器中的方法签名:

public void ReceiveAll([FromBody]ReceiveTime [] monJour)

另外,即使我喜欢法语,我也建议您使用英语来命名变量。不管怎样,请不要被冒犯,但我们真的建议这样做。

答案 2 :(得分:0)

1.添加一个课程,Mesjours:

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

2.在控制器操作中将返回类型更改为异步任务(对于aspnetcore)

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

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

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