我使用fullcalendar作为ASP.NET MVC 4应用程序。激活dayclick后,我有一个bootstrap模式弹出窗口,询问事件标题,日期,时间开始和事件长度。它将创建一个事件,但如果我尝试添加另一个事件,它将返回我在我的代码的“弹出保存”部分中提供的else语句。
这是dayclick功能:
dayClick: function (date, allDay, jsEvent, view) {
$('#eventTitle').val("");
$('#eventDate').val($.fullCalendar.formatDate(date, 'MM/dd/yyyy'));
$('#eventTime').val($.fullCalendar.formatDate(date, 'HH:mm'));
ShowEventPopup(date);
},
显示弹出窗口:
function ShowEventPopup(date) {
ClearPopupFormValues();
$('#popupEventForm').modal('toggle');
$('#eventTitle').focus();
}
function ClearPopupFormValues() {
$('#eventID').val("");
$('#eventTitle').val("");
$('#eventDateTime').val("");
$('#eventDuration').val("");
}
弹出保存:
$('#btnPopupSave').click(function () {
$('#popupEventForm').hide();
var dataRow = {
'Title': $('#eventTitle').val(),
'NewEventDate': $('#eventDate').val(),
'NewEventTime': $('#eventTime').val(),
'NewEventDuration': $('#eventDuration').val()
};
ClearPopupFormValues();
$.ajax({
type: 'POST',
url: "/Calendar/SaveEvent",
data: dataRow,
success: function (response) {
if (response === 'True') {
$('#calendar').fullCalendar('refetchEvents');
alert('New event saved!');
}
else {
alert('Error, could not save event!');
}
}
});
});
创建新事件(在CalendarEvent.cs模型中):
public static bool CreateNewEvent(string Title, string NewEventDate, string NewEventTime, string NewEventDuration)
{
try
{
StoreDbEntities ent = new StoreDbEntities();
Calendar rec = new Calendar();
rec.Title = Title;
rec.DateTimeScheduled = DateTime.ParseExact(NewEventDate + " " + NewEventTime, "MM/dd/yyyy HH:mm", CultureInfo.InvariantCulture);
rec.EventLength = Int32.Parse(NewEventDuration);
ent.Calendars.Add(rec);
ent.SaveChanges();
}
catch (Exception)
{
return false;
}
return true;
}
保存事件(在CalendarController.cs中):
public bool SaveEvent(string Title, string NewEventDate, string NewEventTime, string NewEventDuration)
{
return CalendarEvent.CreateNewEvent(Title, NewEventDate, NewEventTime, NewEventDuration);
}
答案 0 :(得分:1)
当抛出异常时返回false,因此try
块中的某些内容正在搞乱。我猜想当你尝试将事件日期或事件持续时间解析为int
时,会抛出异常。或者您可能尝试将无效数据类型插入上下文。在SaveChanges()
放置一个断点并通过调试器运行应用程序来逐步执行代码,或者另一个明显的选择是查看它给你的异常。