I need to send an array of objects to asp.net mvc 2 through json, but i am not getting null in the mvc controller
The object is like this
entries[1].date = "12/22/2014"
entries[1].Ref = "0002"
entries[1].Credit = "100"
entries[2].date = "12/22/2014"
entries[2].Ref = "0002"
entries[2].Credit = "150"
later before send, i do this
JSON.stringify(entries)
My data in Json looks like this
[{"date":"12/22/2014","RefNo":"0002","Credit":"100"},{"date":"12/22/2014","RefNo":"0002","Credit":"150"}]
my controller in asp.net method looks like this
[HttpPost]
public ActionResult Save(MyDataModel[] entries)
{
}
and my jquery send code
$.ajax({ // create an AJAX call...
type: 'post', // GET or POST
url: url, // the file to call
dataType: 'json',
data: JSON.stringify(entries), // get the form da
})
The MyDataModel is a model class with the properties of the entries
More data
public class MyDataModel
{
public string Date { get; set; }
[Required(ErrorMessage = "RefNo is required", AllowEmptyStrings = false)]
[StringLength(40, ErrorMessage = "RefNo Must be Under 40 characters long", MinimumLength = 1)]
public string RefNo { get; set; }
// Credit
[Required(ErrorMessage = "Credit is required", AllowEmptyStrings = false)]
[Range(typeof(Decimal), "1", "999999999999", ErrorMessage = "Credit Must be a number and non zero")]
public decimal? Credit { get; set; }
}
But the entries is always null, why?
MyDataModel[] entriesshould beString entriesbecause you send a json string.MyDataModel.DataContractJsonSerializerorJavaScriptSerializer?