我遇到了一个奇怪的绑定问题,试图通过 JSON 有效负载在 ajax 发布请求中将列表数据发送到 MVC4 控制器。
我们发送的有效载荷是
{
"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
}
费用清单项目未绑定到以下模型结构
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; }
}
实际的ajax请求如下:
$.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)
});
其中 props.data 是上述 JSON 有效负载。
我一直对此摸不着头脑,看不出任何明显的理由说明为什么费用项目不会受到约束。
有什么想法/建议吗?