如何解析传递给 mvc 控制器的空 json 字符串?

How to resolve null json string passed to mvc controller?

我使用模型对象列表创建了一个 table。 然后在单击该行上的按钮时将行数据传递给 Ajax post。

在 Ajax Post .stringify 中调用传递下来的行数据。 当我在 Dev Tools 中检查在线路上传递的数据值时,我可以看到它们已被填充:

["66", "jdoe@gmail.com", "2009", "test",…]
0
:
"66"
1
:
"jdoe@gmail.com"
2
:
"2009"
3
:
"test"

但是当我进入从客户端调用的控制器时 POST 操作。预期的 JSON 字符串是 null / empty。 我的想法是,这可能是因为 stringify 没有 属性 名称与数组中的每个值关联。

问题:

如何解析传递给 mvc 控制器的空 json 字符串?

下面是实现的要点 -

型号:

public class Status
{

        [Key]
        public int ID { get; set; }


        public string Contact_Email { get; set; }

        public string RID { get; set; }

        public string Name { get; set; }



    }

Table和AJAXpost方法:

     <table id="statusTable" class="table table-hover table-bordered results">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Email</th>
                    <th>RID</th>
                    <th>Name</th>
                    <th>Update Record</th>


                </tr>
            </thead>
            <tbody>
            @foreach (var row in Model.ReleaseStatus)
            {
                    <tr>
                        <td>@Html.Raw(row.ID)</td>
                        <td>@Html.Raw(row.Contact_Email_Name)</td>
                        <td>@row.RID</td>
                        <td>@row.Name</td>
                        <td><button type="submit" class="btn btn-success">Update</button></td>
                    </tr>
              }
            </tbody>

        </table>



 $(".btn-success").click(function () {
            var $td = $(this).closest('tr').children("td"),
            len = $td.length;
            var tableData = $td.map(function (i) {
                if (i < len - 1)
                    return $(this).text();
            }).get();

            console.log(tableData);

            //Post JSON data to controller
            $.ajax({
                type: "POST",
                url: 'updateStatus',
                data: JSON.stringify(tableData),
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    console.log("post success");
                },
                error: function (request) {
                    console.log("post error" + request.error);
                }

            });


        });

最后是 MVC 控制器中的 POST 方法:

    [HttpPost]
    public ActionResult updateStatus(stirng jsonString)
    {
        //deserialise the json to a Status object here
    }

需要创建您的控制器以接受实际对象,而不是 json 字符串。

我的意思是

[HttpPost]
public ActionResult updateStatus(stirng jsonString)
{
   //deserialise the json to a Status object here
}

应该是

[HttpPost]
public ActionResult updateStatus(Status vm)
{
   //no need to deserialize - was already done by the model binder
}

为了让模型绑定器将您的 json 绑定到 Status,您需要传递一个 json 对象来复制您的视图模型。

{
 ID:yourId, 
 Contact_Email:yourContactEmail, 
 RID:YourRID, 
 Name:yourName
}

在伪代码中,您可以:

var statusData = {
     ID:tableData[0], 
     Contact_Email:tableData[1],
     RID:tableData[2], 
     Name:tableData[3]
};

然后在您的 ajax 电话中,

data: JSON.stringify(statusData),

试试 from body 属性

[HttpPost]
public ActionResult updateStatus([FromBody]Status status)
{
    //add the serialize attribute on your model 
}