• <bdo id='HaB3P'></bdo><ul id='HaB3P'></ul>
    <legend id='HaB3P'><style id='HaB3P'><dir id='HaB3P'><q id='HaB3P'></q></dir></style></legend>
    <tfoot id='HaB3P'></tfoot>

      <small id='HaB3P'></small><noframes id='HaB3P'>

      1. <i id='HaB3P'><tr id='HaB3P'><dt id='HaB3P'><q id='HaB3P'><span id='HaB3P'><b id='HaB3P'><form id='HaB3P'><ins id='HaB3P'></ins><ul id='HaB3P'></ul><sub id='HaB3P'></sub></form><legend id='HaB3P'></legend><bdo id='HaB3P'><pre id='HaB3P'><center id='HaB3P'></center></pre></bdo></b><th id='HaB3P'></th></span></q></dt></tr></i><div id='HaB3P'><tfoot id='HaB3P'></tfoot><dl id='HaB3P'><fieldset id='HaB3P'></fieldset></dl></div>
      2. ASP.NET Core 发布数组对象 JSON

        时间:2023-07-10

          1. <small id='vX0dI'></small><noframes id='vX0dI'>

              <tbody id='vX0dI'></tbody>
                <bdo id='vX0dI'></bdo><ul id='vX0dI'></ul>
                • <tfoot id='vX0dI'></tfoot>
                • <i id='vX0dI'><tr id='vX0dI'><dt id='vX0dI'><q id='vX0dI'><span id='vX0dI'><b id='vX0dI'><form id='vX0dI'><ins id='vX0dI'></ins><ul id='vX0dI'></ul><sub id='vX0dI'></sub></form><legend id='vX0dI'></legend><bdo id='vX0dI'><pre id='vX0dI'><center id='vX0dI'></center></pre></bdo></b><th id='vX0dI'></th></span></q></dt></tr></i><div id='vX0dI'><tfoot id='vX0dI'></tfoot><dl id='vX0dI'><fieldset id='vX0dI'></fieldset></dl></div>
                • <legend id='vX0dI'><style id='vX0dI'><dir id='vX0dI'><q id='vX0dI'></q></dir></style></legend>
                  本文介绍了ASP.NET Core 发布数组对象 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将视图中的 Object 数组发布到我的控制器,但参数为 null 我发现对于一个简单的对象,我需要将 [FromBody] 放入我的控制器操作中.

                  I'm trying to post an array of Object in my view to my controller but params are null i saw that for just a simple object I need to put [FromBody] in my controller action.

                  这是我的 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 请求:

                  Here is my Ajax Request :

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

                  任何帮助表示赞赏:)

                  推荐答案

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

                  Instead of using ReceiveTime[] rt, it might be better to model the data according to the same structure that you POST. For example, you could create a class that looks like this:

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

                  我不知道将 MesJours 作为班级名称是否有意义(我不会说法语),但想法仍然很明确 - 您可以随意命名班级.

                  I don't know if MesJours makes sense as a class name (I don't speak French), but the idea is still clear - You can name the class whatever you like.

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

                  Given this, you could update your controller like this:

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

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

                  This would satisfy the ASP.NET MVC Model Binder and should give you the data you've POSTed. This also has the added benefit of easily accommodating any additional properties you might want to POST.

                  这篇关于ASP.NET Core 发布数组对象 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何找到所有控制器和动作 下一篇:在 asp.net mvc core 中绑定 Guid 参数

                  相关文章

                  • <bdo id='nAt4k'></bdo><ul id='nAt4k'></ul>
                  <legend id='nAt4k'><style id='nAt4k'><dir id='nAt4k'><q id='nAt4k'></q></dir></style></legend><tfoot id='nAt4k'></tfoot>

                      <small id='nAt4k'></small><noframes id='nAt4k'>

                      <i id='nAt4k'><tr id='nAt4k'><dt id='nAt4k'><q id='nAt4k'><span id='nAt4k'><b id='nAt4k'><form id='nAt4k'><ins id='nAt4k'></ins><ul id='nAt4k'></ul><sub id='nAt4k'></sub></form><legend id='nAt4k'></legend><bdo id='nAt4k'><pre id='nAt4k'><center id='nAt4k'></center></pre></bdo></b><th id='nAt4k'></th></span></q></dt></tr></i><div id='nAt4k'><tfoot id='nAt4k'></tfoot><dl id='nAt4k'><fieldset id='nAt4k'></fieldset></dl></div>