我有一个接收JSON对象的Web API方法。
客户端发送的JSON是:
{
"EmployerId": 1,
"EmployeeId": 6,
"HolidayTypeId": 1,
"CategoryType": "Normal",
"SelectedDates": {
"Wed Jul 16 2014": "2014-07-16T00:00:00",
"Thu Jul 17 2014": "2014-07-17T00:00:00",
"Fri Jul 18 2014": "2014-07-18T00:00:00"
}
}
Web Method是这样的:
[HttpPost]
public HttpResponseMessage UpdateHolidayRequest([FromBody] Model.HolidayRequestModel request) {
HttpResponseMessage returnVal = null;
returnVal = Request.CreateResponse(HttpStatusCode.OK, new Model.Custom.JsonResponse {
data = null,
message = "",
num = 1,
success = true
});
}
我的HolidayRequestModel
类是:
public class HolidayRequestModel {
public int HolidayId {
get;
set;
}
public int EmployeeId {
get;
set;
}
public byte HolidayTypeId {
get;
set;
}
// Selected Dates
public List<string> SelectedDates {
get;
set;
}
}
我得到了所有的属性,除了SelectedDates
,因为我意识到这不是一个List<string>
,因为SelectedDates
有一个字段和一个值。
关于SelectedDates
属性类型应该是什么以便在我的模型中填充的任何线索?
将SelectedDates
属性更改为Dictionary<string, string>
(或可能是Dictionary<string, DateTime>
)而不是List<string>
,这将允许它正确地反序列化。
。:
public class HolidayRequestModel {
...
// Selected Dates
public Dictionary<string, string> SelectedDates {
get;
set;
}
}