[英]Send array of Objects to MVC Controller
I try to send an array of json objects to server: 我尝试将一个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# 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
}
The dataType
property is saying what you expect back from the server. dataType
属性表示您希望从服务器返回的内容。 Try setting the contentType
: 尝试设置contentType
:
contentType: 'application/json'
Try 尝试
data: JSON.stringify({ pos: @objectData })
Also, check what is being rendered in the View through the browser. 另外,通过浏览器检查View中正在呈现的内容。 The reason you are getting null is likely because JavaScript is not getting a proper value. 您获得null的原因很可能是因为JavaScript没有获得正确的值。
function SendArrayOfObjects() { var things = [{ id: 1, color: 'red' }, { id: 2, color: 'blue' }, { id: 3, color: 'yellow' }]; function SendArrayOfObjects(){var things = [{id:1,color:'red'},{id:2,color:'blue'},{id:3,color:'yellow'}];
$.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!");
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.