I've been getting a weird binding issue trying to send list data to an MVC4 controller in an ajax post request via JSON payload.
The payload we're sending is
{
"assignmentId":"AssignmentId2",
"shiftId":null,
"startDate":null,
"startTime":{
"hours":0,
"minutes":0
},
"endTime":{
"hours":0,
"minutes":0
},
"breaksDuration":{
"hours":0,
"minutes":0
},
"authorised":false,
"authorisedName":null,
"mileageDescription":null,
"mileage":0,
"expenses":[
{
"description":"DADADDAADADADAD",
"total":"5"
}
],
"billableDuration":{
"hours":0,
"minutes":0
},
"expensesComplete":true,
"expensesTotal":5
}
The expenses list items are not getting bound to the following model structure
public class ShiftApiModel
{
public string assignmentId { get; set; }
public string shiftId { get; set; }
[Required]
public DateTime startDate { get; set; }
[Required]
public ShortTimeSpan startTime { get; set; }
[Required]
public ShortTimeSpan endTime { get; set; }
public bool authorised { get; set; }
public string authorisedName { get; set; }
public ShortTimeSpan breaksDuration { get; set; }
public decimal mileage { get; set; }
public string mileageDescription { get; set; }
private IList<ExpenseApiModel> _expenses = new List<ExpenseApiModel>();
public IList<ExpenseApiModel> expenses { get { return _expenses; } set { _expenses = value; } }
}
public class ExpenseApiModel
{
public string description { get; set; }
public double total { get; set; }
}
The actual ajax request is as follows:
$.ajax({
type: type,
url: serviceUrl,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: (props.data) ? props.data : null,
success: function (jqXHR, textStatus) {
this.serviceCallComplete(jqXHR, props.complete, props.error);
}.bind(this),
error: function (jqXHR, textStatus, errorThrown) {
this.serviceCallFailure(jqXHR, props.error);
}.bind(this)
});
Where props.data is the JSON payload described above.
I've been scratching my head on this and can't see any obvious reason as to why the expenses item wouldn't be getting bound.
Any ideas/ suggestions ?