Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send array of Objects to MVC Controller

I try to send an array of json objects to server:

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       
}
like image 455
daniel Avatar asked Apr 30 '26 05:04

daniel


1 Answers

The dataType property is saying what you expect back from the server. Try setting the contentType:

contentType: 'application/json'
like image 180
Justin Helgerson Avatar answered May 03 '26 21:05

Justin Helgerson