首页 文章

将时间格式从完整日历开始时间转换为C#时间 Span 时出错

提问于
浏览
-1

我试图将fullcalendar返回的事件开始时间转换为动作方法中的c#timespan对象的模型绑定 . fullcalendar将值分配给开始时间的格式是“2015年5月24日上午6:30” . 但是在ModelState中我得到以下错误“值'2015年5月24日上午6:30'对于StartTime无效 . ”在模式中我必须以上述格式显示开始时间(2015年5月24日上午6:30) . 但是希望它能够正确地将绑定模型化为Timespan对象 . 对此有任何帮助 .

有关该方案的更多信息:

这是我在日历javascript中显示自定义日期时间格式$("#StartTimeTxtBox").val(moment(start).format('LLL'));显示时间为'May 24, 2015 6:30 AM' . 我试图在我的动作方法中将其绑定到"public TimeSpan StartTime { get; set; }",它会导致错误"The value 'May 24, 2015 6:30 AM' is not valid for 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);
            }

1 回答

  • 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'));
    

相关问题