将FUllCalendar事件传递给控制器方法
本文关键字:控制器 方法 FUllCalendar 事件 | 更新日期: 2023-09-27 18:13:13
我正试图将fullCalendar
事件发布到我的控制器动作方法,但由于某种原因,我从视图传递到控制器的json将为空。我尝试了所有可能的组合,比如改变动作参数到我的模型类型,到字符串类型,甚至传递简单的杰森,所有的来作为空在我的动作参数。这是我的Ajax代码和控制器方法,我相信这是一个小问题,但我不能弄清楚我做错了什么。
$("#calendar").fullCalendar({
header: {
left: 'prevYear,prev,next,nextYear',
center: 'title',
right: 'month,agendaWeek,agendaDay',
},
defaultView: 'month',
editable: true,
alldayslot: false,
selectable: true,
slotminutes: 15,
nextDayThreshold: "00:00:00",
events: "/Home/FullCalendar",
eventDrop: function(event, delta, revertFunc)
{
if (confirm("Even changed")) {
Update(event);
}
else revertFunc();
},
});
function Update(event) {
var datarow = new Object();
datarow.TaskId = event.id;
datarow.StartDate = event.start;
datarow.EndDate = event.end;
debugger;
$.ajax({
type: "post",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(datarow),
url: "/Home/UpdateTask",
success: function (data) {
debugger;
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
debugger;
}
});
}
});
My Controller方法
[HttpPost]
public void UpdateTask(Tasks td)
{
// Update event in database
}
这是我最初如何工作的(现在改变了很多,伟大的插件是FullCalendar)。
$(document).ready(function () {
var calendar = $('#calendar').fullCalendar({
events: '/BookingTwo/GetDiaryEvents/',
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultView: 'month',
editable: false,
contentHeight: 700,
selectable: false,
eventClick: function (event, jsEvent, view) {
var mywhen = moment(event.start).format('MMM Do h:mm A') + ' - ' + moment(event.end).format('MMM Do h:mm A');
$("#startTime").html(moment(event.start).format('MMM Do h:mm A'));
$("#endTime").html(moment(event.end).format('MMM Do h:mm A'));
$('#modalTitle').html(event.title);
$('#modalBody').html(event.description);
$('#etad').html(event.etad);
$('#etadsub').html(event.etadsub);
$('#when').text(mywhen);
$('#fullCalModal').modal();
},
dayClick: function (date, allDay, jsEvent, view) {
var mywhen = moment(event.start).format('MMM Do h:mm A') + ' - ' + moment(event.end).format('MMM Do h:mm A');
$("#customId").html(moment(event.start).format('MMM Do h:mm A'));
$("#LeaveStart").html(moment(event.start).format('MMM Do h:mm A'));
$("#LeaveFinish").html(moment(event.end).format('MMM Do h:mm A'));
$('#title').html(event.title);
$('#StaffID').html(event.userId);
$('#selectrole').html(event.RowId);
$('#customdescription').html(event.description);
$('#sicktype').html(event.ETADType);
$('#sicksub').html(event.ETADSubType);
$('#eventstatus').html(event.EventStatus);
$('#when').text(mywhen);
$('#popupEventForm').modal();
}
});
$('#btnPopupCancel').click(function () {
ClearPopupFormValues();
$('#popupEventForm').hide();
});
$('#btnPopupSave').click(function () {
$('#popupEventForm').hide();
var dataRow = {
'Id': $('#customId').val(),
'Subject': $('#title').val(),
'Description': $('#customdescription').val(),
'StartTime': $('#LeaveStart').val(),
'EndTime': $('#LeaveFinish').val(),
'SelectRole': $('#SelectRole').val(),
//'Recurrence': $('#eventDuration').val(),
//'AllDay': $('#eventDuration').val(),
//'AllRoles': $('#eventDuration').val(),
'ETADType': $('#SicknessType').val(),
'ETADSubType': $('#SicknessSubType').val(),
//'RecurrenceRule': $('#eventDuration').val(),
//'EventDate': $('#eventDuration').val(),
'StaffID': $('#StaffID').val(),
//'EventStatus': $('#EventStatus').val()
//'Duration': $('#eventDuration').val(),
//'colourId': $('#eventDuration').val()
}
ClearPopupFormValues();
$.ajax({
type: 'POST',
url: "/BookingTwo/SaveEvent",
data: dataRow,
success: function (response) {
if (response == 'True') {
$('#calendar').fullCalendar('refetchEvents');
alert('New event saved!');
}
else {
alert('Error, could not save event!');
}
}
});
});
function ShowEventPopup(date) {
ClearPopupFormValues();
$('#popupEventForm').show();
$('#title').focus();
}
function ClearPopupFormValues() {
$('#customId').val("");
$('#title').val("");
$('#LeaveStart').val("");
$('#LeaveFinish').val("");
}
function UpdateEvent(EventID, EventStart, EventEnd) {
var dataRow = {
'id': EventID,
'NewEventStart': EventStart,
'NewEventEnd': EventEnd
}
$.ajax({
type: 'POST',
url: "/BookingTwo/UpdateEvent",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(dataRow)
});
}
});
这里是当前控制器动作
[HttpPost]
public ActionResult SaveEvent(DiaryEvent events)
{
try
{
int submit = bookingService.Insert(events);
//get record here to check if inserted
if(submit > 0)
{
}
}
catch(Exception ex)
{
ErrorLog.Capture(ex);
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
//return some shit
}
return new HttpStatusCodeResult(HttpStatusCode.Created);
}