如何使用插入的对象发布json对象? 我的代码: 型号代码:
public class MyModel
{
public string Text { set; get; }
public int Param { set; get; }
public InsertedModel insert { set; get; }
}
public class InsertedModel
{
public string InsertedString { set; get; }
}
JavaScript代码:
<script>
function createPost() {
$.ajax({
type: "POST", url: "/",
success: function (data) { alert('data: ' + data); },
data: { "Text": "Some Name", "Param": 30, "insert": { "InsertedString": "123" } },
accept: 'application/json'
});
}
</script>
控制器代码:
[HttpPost]
public void Index(Models.MyModel postReq)
{
//breakpoint
}
在断点中我看到:
Param: 30
Text: "Some Name"
insert: {Models\InsertedModel}
insert.InsertedString: null
请帮帮我。我需要做什么呢?如何在C#HTTPPost函数中看到我的所有JSON结构?
答案 0 :(得分:2)
您只是传递对象而不是JSON。最好将此对象转换为字符串,并将内容类型指定为JSON:
var requestData = JSON.stringify({ "Text": "Some Name", "Param": 30, "insert": { "InsertedString": "123" } });
function createPost() {
$.ajax({
type: "POST", url: "/",
success: function (data) { alert('data: ' + data); },
contentType: 'application/json',
data: requestData,
accept: 'application/json'
});
}