Skip to main content
 首页 » 编程设计

c#之如何解析传递给 mvc Controller 的空 json 字符串

2024年02月05日2098°冷暖

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

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

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

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

问题:

如何解析传递给 mvc Controller 的空 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; } 
 
 
 
    } 

表格和AJAX post方法:

     <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 Controller 中的 POST 方法:

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

请您参考如下方法:

需要创建您的 Controller 以接受实际对象,而不是 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 
} 

为了让模型绑定(bind)器将您的 json 绑定(bind)到 Status,您需要传递一个复制您的 View 模型的 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),