2015-05-24 33 views
-1

我試圖將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); 
      } 

+0

我可以將時間跨度轉換爲日期時間在這種情況下,但仍gaiazov提到的,仍然不能正確地綁定 – user1744973

+0

'HH :mm'格式應正確反序列化爲'TimeSpan' – gaiazov

回答

0

您需要使用LThh:mm momentjs格式代替LLL。 LLL格式包含日期組件,您只需要時間組件。

例如改變

$("#StartTimeTxtBox").val(moment(start).format('LLL')); 
$("#EndTimeTxtBox").val(moment(end).format('LLL')); 
$('#AppointmentDate').val(moment(start).format('YYYY-MM-DD hh:mm:ss')); 

$("#StartTimeTxtBox").val(moment(start).format('LT')); 
$("#EndTimeTxtBox").val(moment(end).format('LT')); 
$('#AppointmentDate').val(moment(start).format('YYYY-MM-DD')); 

$("#StartTimeTxtBox").val(moment(start).format('hh:mm')); 
$("#EndTimeTxtBox").val(moment(end).format('hh:mm')); 
$('#AppointmentDate').val(moment(start).format('YYYY-MM-DD')); 
+0

但是這不會顯示在上述要求格式(2015年5月24日6:30 AM)。以任何方式顯示在上述方式和模型綁定它以你的方式。或者在操作方法中使用分隔符唯一的選項。 – user1744973

+0

在這種情況下,您可以使用隱藏字段作爲想要發送到服務器的值 – gaiazov

+0

因此,爲了讓事情變得有點:),我還爲這些文本框使用了bootstrap-datetimepicker,以便用戶還可以更改值使用datetimepicker,但又以相同的格式。所以根據你的解決方案,我應該使用jQuery事件以適當的格式複製一些隱藏字段中的文本框的值,然後模型綁定它? – user1744973

相關問題