我试图将fullcalendar返回的事件开始时间转换为使用action方法中的c#timespan对象进行模型绑定。 fullcalendar将值分配给开始时间的格式是“2015年5月24日上午6:30”。但是在ModelState中我得到以下错误“值'2015年5月24日上午6:30'对于StartTime无效。”在模式中我必须以上述格式显示开始时间(2015年5月24日上午6:30)。但是希望它能够正确地将绑定模型化为Timespan对象。对此有任何帮助。
有关该方案的更多信息:
这是我在日历javascript中显示的代码,用于显示自定义日期时间格式
$( “#StartTimeTxtBox”)VAL(力矩(开始).format( 'LLL'));显示时间为'2015年5月24日上午6:30'。我试图在我的动作方法中将其绑定到“公共TimeSpan StartTime {get; set;}”并导致错误“值'2015年5月24日上午6:30'对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(); }
StartTime文本框的标记:
<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 :(得分:0)
您需要使用LT
或hh:mm
momentjs格式而不是LLL
。 LLL格式包括日期组件,您只需要时间组件。
e.g。变化
$("#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'));