我尝试将一个json对象数组发送到服务器:
var objectData = [
{ Description: "Bezeichnung", Value: "1", Name: "Betrag (Brutto)" },
{ Description: "Dies ist die erste Bezeicnung", Value: "101", Name: "11,90" },
{ Description: "Dies ist die zweite Bezeicnung", Value: "12", Name: "11,90" }
];
$.ajax({
url: "/system/createinvoice",
data: JSON.stringify({ pos: objectData }) ,
dataType: 'json',
type: 'POST',
});
C#
public class InvoicePos
{
public string Description { get; set; }
public Nullable<double> Value { get; set; }
public string Name { get; set; }
}
[POST("/system/createinvoice")]
public void newquestion2(InvoicePos[] pos)
{
// pos is always null
}
答案 0 :(得分:6)
dataType
属性表示您希望从服务器返回。尝试设置contentType
:
contentType: 'application/json'
答案 1 :(得分:0)
尝试
data: JSON.stringify({ pos: @objectData })
此外,通过浏览器检查View中正在呈现的内容。您获得null的原因可能是因为JavaScript没有获得正确的值。
答案 2 :(得分:0)
function SendArrayOfObjects() { var things = [{id:1,color:&#39; red&#39; },{id:2,颜色:&#39;蓝色&#39; },{id:3,颜色:&#39;黄色&#39; }];
$.ajax({
type: "POST",
url: "<%= ResolveUrl("~/MyServices.aspx/GetData")%>",
data: JSON.stringify({ objdata: things }),
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function()
{
$("#msg").html("data sent successfully!");
},
error: function()
{
$("#msg").html(" Can not send data!");
}
});
}