c# - ASP.NET Core 发布数组对象 JSON

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

我正在尝试将我 View 中的对象数组发布到我的 Controller ,但参数为空我看到对于一个简单的对象我需要将 [FromBody] 放在我的 Controller 操作中。

这是我的 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) { }

这是我的类(class):

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

感谢任何帮助:)

最佳答案

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

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

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

鉴于此,您可以像这样更新您的 Controller :

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

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

关于c# - ASP.NET Core 发布数组对象 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47731755/

相关文章:

c# - 具有XML数据源的Ext.NET GridPanel

c# - 当我更改计算机日期格式并且无法检索日期选择器值时,ASP.NET MVC Core DateTime 表现得很奇怪

c# - 如何使用 .NET Core MVC 框架将 TwiML 实现为一种方法?

c# - 无法使用 whis json 反序列化当前 JSON 对象

c# - 在两个日期范围之间相交的天数

postgresql - 将 PostgreSQL 安装到 asp.net core 容器中

asp.net-core - 为什么 CsvHelper 不从 MemoryStream 读取?

asp.net-core - .Net核心 View : the name "Layout" does not exist in the current context

asp.net - 重新部署 ASP.NET Core MVC 网站后,Azure SQL 数据库列未更新

c# - 关于使用 Botframework v4 更改为新 LUIS key 的问题