我試圖將fullcalendar返回的事件開始時間轉換爲模型與操作方法中的c#timespan對象綁定。 fullcalendar將值分配給開始時間的格式爲「2015年5月24日6:30 AM」。但是在ModelState即時獲取以下錯誤「價值'2015年5月24日6:30 AM'對於StartTime無效」。在這種情況下,我必須以上述格式顯示開始時間(2015年5月24日6:30 AM)。但希望它將模型正確綁定到Timespan對象。任何關於此的幫助。將時間格式從完整的日曆開始時間轉換爲C#時間間隔的錯誤
關於方案的一些詳細信息:
這是代碼我在日曆JavaScript來顯示自定義日期時間格式 $( 「#StartTimeTxtBox」)VAL(力矩(開始).format('LLL。 「));顯示時間爲'2015年5月24日6:30 AM'。我試圖在我的操作方法中將此綁定到「public TimeSpan StartTime {get; set;}」,並導致錯誤「The value'2015年5月24日6:30 AM'對於StartTime無效」。有什麼辦法在綁定到服務器變量之前進行轉換? enter code here
更新#1:我發佈的代碼部分更清晰。
在完整的日曆的javascript:
select: function (start, end, allDay) { $('#modalTitle').html("Add a new event"); //$('#modalBody').html("Hello world"); $("#StartTimeTxtBox").val(moment(start).format('LLL')); $("#EndTimeTxtBox").val(moment(end).format('LLL')); $('#AppointmentDate').val(moment(start).format('YYYY-MM-DD hh:mm:ss')); $("#PatientStatement").val('Please add some description about illness'); $("#DoctorClinicID").val('@ViewBag.DoctorClinicID'); $('#calendarModal').modal(); calendar.fullCalendar('unselect'); refreshCalendar(); }
標記的開始時間文本框: <input type='text' id="StartTimeTxtBox" class="form-control sharp-edge" placeholder="Start Time" name="StartTime" />
C#視圖模型:
public class AppointmentModel { public int AppointmentID { get; set; } public int DoctorClinicID { get; set; } public string DoctorName { get; set; } public DateTime AppointmentDate { get; set; } public TimeSpan StartTime { get; set; } public TimeSpan EndTime { get; set; } public DateTime Start { get { var ret = AppointmentDate; ret = ret.Add(StartTime); return ret; } } public DateTime End { get { var ret = AppointmentDate; ret = ret.Add(EndTime); return ret; } } public int PatientID { get; set; } public string PatientName { get; set; } public AppointmentStatus Status { get; set; } public int TokenNumber { get; set; } public string PatientStatement { get; set; } public string ClinicComments { get; set; } }
操作方法:
public JsonResult SaveAppointment(AppointmentModel appointmentModel) { int appointmentID = 0; if (ModelState.IsValid) { Appointment appointment = new Appointment { AppointmentDate = appointmentModel.AppointmentDate, StartTime = appointmentModel.StartTime, EndTime = appointmentModel.EndTime, AppointmentStatusID = 1, ClinicComments = appointmentModel.ClinicComments, DoctorClinicID = appointmentModel.DoctorClinicID, PatientID = appointmentModel.PatientID, PatientStatement = appointmentModel.PatientStatement, }; appointmentID = repository.SaveAppointment(appointment); } return Json(appointmentID, JsonRequestBehavior.AllowGet); }
我可以將時間跨度轉換爲日期時間在這種情況下,但仍gaiazov提到的,仍然不能正確地綁定 – user1744973
'HH :mm'格式應正確反序列化爲'TimeSpan' – gaiazov