当我试图从post方法中获取对象时,我无法获得DateTime的正确值,我的JSON:
{
"JobRecord": [{
"ShiftRecordID": 2,
"JobDescriptionID": 0,
"TaskDesciption": "Test task description",
"WorkedWith": "I,G",
"StartTime": "2015-02-06T07:30:00",
"EndTime": "2015-02-06T09:00:00",
"JobDescription": "Exit & Emergency Remedials",
"JobNumber": 11939
}],
"EmployeeID": 1,
"isCompanyCar": false
}
StartTime和EndTime的值总是变成0001/01/01 00:00:00,有谁知道问题出在哪里?我在这里呆了2天,找不到任何相关的解决方案。
班级定义
public class JobRecordJson
{
public int JobDescriptionID { get; set; }
public string TaskDescription { get; set; }
public string WorkedWith { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public int EmployeeID { get; set; }
public bool IsCompanyCar { get; set; }
public DayType DayType { get; set; }
}
控制器
[ResponseType(typeof(JobRecord))]
public IHttpActionResult PostJobRecord(JobRecordJson jobRecord)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Debug.WriteLine(jobRecord);
//DateTime.Parse(jobRecord.StartTime);
// check if a shift record was created
var shiftRecord = from r in db.ShiftRecords
where r.RecordDate == jobRecord.StartTime.Date && r.EmployeeID == jobRecord.EmployeeID
select r;
try
{
int shiftRecordID;
// if there isn't, create shift record first
if (shiftRecord.Count() < 1)
{
int maxRecordID = db.ShiftRecords.Max(m => m.ID);
ShiftRecord sr = new ShiftRecord()
{
ID = maxRecordID + 1,
EmployeeID = jobRecord.EmployeeID,
IsCompanyVehicle = jobRecord.IsCompanyCar,
RecordDate = jobRecord.StartTime.Date,
DayType = jobRecord.DayType,
IsRead = false
};
db.ShiftRecords.Add(sr);
db.SaveChanges();
shiftRecordID = sr.ID;
}
else
{
shiftRecordID = shiftRecord.First().ID;
}
// create job record
int maxJobRecordID = db.JobRecords.Max(m => m.ID);
JobRecord jr = new JobRecord()
{
ID = maxJobRecordID + 1,
ShiftRecordID = shiftRecordID,
JobDescriptionID = jobRecord.JobDescriptionID,
TaskDesciption = jobRecord.TaskDescription,
WorkedWith = jobRecord.WorkedWith,
StartTime = jobRecord.StartTime,
EndTime = jobRecord.EndTime,
};
db.JobRecords.Add(jr);
db.SaveChanges();
}
catch (Exception ex)
{
Debug.WriteLine("There is an error: " + ex.ToString());
return InternalServerError();
}
return Ok(jobRecord);
}
因为我想在没有任何ShiftRecord的情况下自动创建ShiftRecord,所以我创建了一个JobRecordJson类(它是一个DTO类,基本上包含一个JobRecord类和一些我需要的额外信息)。 获得JobRecordJson对象后,我将创建JobRecord项和ShiftRecord(如果需要)
public class JobRecord
{
public int ID { get; set; }
public int JobDescriptionID { get; set; }
public int ShiftRecordID { get; set; }
[Display(Name="Task")]
public string TaskDesciption { get; set; }
[Display(Name="Worked With")]
public string WorkedWith { get; set; }
[Display(Name="Start Time")]
public DateTime StartTime { get; set; }
[Display(Name="End Time")]
public DateTime EndTime { get; set; }
public virtual JobDescription JobDescription { get; set; }
public virtual ShiftRecord ShiftRecord { get; set; }
}
public class ShiftRecord
{
public int ID { get; set; }
public int EmployeeID { get; set; }
[Display(Name = "Vehicle Type")]
public bool IsCompanyVehicle { get; set; }
[DataType(DataType.Date)]
[Display(Name = "Date")]
[DisplayFormat(DataFormatString="{0:yyyy-MM-dd}", ApplyFormatInEditMode=true)]
public DateTime RecordDate { get; set; }
[Display(Name="Day Type")]
public DayType? DayType { get; set; }
// properties below can only be operated by administrator
[Display(Name = "Normal Hrs")]
public decimal? NormalHours { get; set; }
[Display(Name = "Time And Half Hrs")]
public decimal? TimeAndHalfHours { get; set; }
[Display(Name = "Double Time Hrs")]
public decimal? DoubleTimeHours { get; set; }
[Display(Name = "Shift Hrs")]
public decimal? ShiftHours { get; set; }
[Display(Name="Comment")]
public string Comment { get; set; }
[Display(Name="Read?")]
public bool IsRead { get; set; }
public virtual Employee Employee { get; set; }
public virtual ICollection<JobRecord> JobRecords { get; set; }
}
答案 0 :(得分:1)
问题是你的json数据结构,默认的模型绑定器不能将该结构绑定到JobRecordJson。 您需要定义自己的自定义模型绑定器或将json更改为以下内容:
{
"ShiftRecordID": 2,
"JobDescriptionID": 12,
"TaskDesciption": "Test task description",
"WorkedWith": "I,G",
"StartTime": "2015-02-06T07:30:00",
"EndTime": "2015-02-06T09:00:00",
"JobDescription": "Exit & Emergency Remedials",
"JobNumber": 11939,
"EmployeeID": 1,
"isCompanyCar": false
}
答案 1 :(得分:0)
我认为这可能是你的Json。你能尝试将你的属性名称设置为驼峰案吗?或者甚至更好地创建一个web api方法,该方法返回对象的一个示例,并查看它返回的内容。
可能的原因属性不正确: 示例&#34; startTime&#34;:&#34; 2015-02-06 ...&#34;而不是&#34; StartTime&#34;。这可能是所有值都为空的原因。
如果你在帖子中看到一个空对象,你可能会有一个无效的结构,从我可以告诉你的Json应该开始:
{ ... &#34; STARTTIME&#34;:&#34; 2015 .... }