|
$(function () {
var dataManager = ej.DataManager({
url: "/Home/GetData",
adaptor: new ej.UrlAdaptor()
});
$("#Schedule1").ejSchedule({
currentView: "month",
currentDate: new Date(2016, 10, 29),
appointmentSettings: {
dataSource: dataManager,
id: "Id",
subject: "Subject",
startTime: "StartTime",
endTime: "EndTime",
startTimeZone: "StartTimeZone",
endTimeZone: "EndTimeZone",
allDay: "AllDay",
recurrence: "Recurrence",
recurrenceRule: "RecurrenceRule"
},
beforeAppointmentCreate: "onCRUDActions",
beforeAppointmentChange: "onCRUDActions",
beforeAppointmentRemove: "onCRUDActions"
})
});
function onCRUDActions(args) {
args.cancel = true; var UserData;
if (args.currentAction == "add" || args.type == "beforeAppointmentCreate") {
var appoint = [];
appoint[0] = !(ej.isNullOrUndefined(args.appointment[0])) ? args.appointment[0] : args.appointment;
UserData = { "added": appoint, "changed": null, "removed": null };
}
else if (args.currentAction == "edit" || args.currentAction == "editSeries") {
var appoint = [];
if (args.currentAction == "editSeries") {
var app = new ej.DataManager(this._currentAppointmentData).executeLocal(new ej.Query().where("ParentId", ej.FilterOperators.equal, args.appointment.changed[0].ParentId));
for (var i = 0 ; i < app.length ; i++) {
if (app[i][this._appointmentSettings["recurrenceRule"]].indexOf("RECUREDITID") != -1) {
appoint.push(app[i]);
}
}
}
UserData = { "added": null, "changed": args.appointment.changed, "removed": (appoint.length > 0) ? appoint : null };
}
else if (args.currentAction == "delete" || args.currentAction == "deleteSeries") {
var appoint = new ej.DataManager(this._currentAppointmentData).executeLocal(new ej.Query().where("ParentId", ej.FilterOperators.equal, args.appointment.ParentId));
UserData = { "added": null, "changed": null, "removed": appoint };
}
else if (args.currentAction == "editOccurrence") {
var appoint = new ej.DataManager(this._currentAppointmentData).executeLocal(new ej.Query().where("ParentId", ej.FilterOperators.equal, args.appointment.changed[0].ParentId));
args.appointment.changed[0]["Recurrence"] = true;
UserData = { "added": args.appointment.changed, "changed": appoint, "removed": null };
}
else if (args.currentAction == "deleteOccurrence") {
var appoint = new ej.DataManager(this._currentAppointmentData).executeLocal(new ej.Query().where("ParentId", ej.FilterOperators.equal, args.appointment.ParentId));
UserData = { "added": null, "changed": appoint, "removed": null };
}
$.ajax({
url: '/Home/CRUD/',
data: JSON.stringify(UserData),
type: 'POST',
traditional: true,
contentType: 'application/json',
success: function (data) {
if (data.status && data.status != "") {
var obj = $("#Schedule1").data("ejSchedule");
obj.refreshAppointments();
alert(data.status);
}
},
failure: function (data) {
alert("Failure occurred")
},
error: function (data) {
alert("Error Occurred")
}
});
} |
|
[HttpPost]
public JsonResult CRUD(BatchData userdata)
{
var status = "";
if (userdata.added.Count > 0)
{
var value1 = userdata.added[0];
int intMax = db.ScheduleDatas.ToList().Count > 0 ? db.ScheduleDatas.ToList().Max(p => p.Id) : 1;
DateTime startTime = Convert.ToDateTime(value1.StartTime);
DateTime endTime = Convert.ToDateTime(value1.EndTime);
var currentTimeZone = TimeZone.CurrentTimeZone;
ScheduleData appoint = new ScheduleData()
{
Id = intMax + 1,
StartTime = startTime,
EndTime = endTime,
StartTimeZone = value1.StartTimeZone,
EndTimeZone = value1.EndTimeZone,
Subject = value1.Subject,
Recurrence = value1.Recurrence,
AllDay = value1.AllDay,
RecurrenceRule = value1.RecurrenceRule
};
db.ScheduleDatas.InsertOnSubmit(appoint);
db.SubmitChanges();
status = "Appointment inserted successfully";
}
if (userdata.removed.Count > 0)
{
foreach (var apps in userdata.removed)
{
ScheduleData app = db.ScheduleDatas.Where(c => c.Id == apps.Id).FirstOrDefault();
if (apps != null) db.ScheduleDatas.DeleteOnSubmit(app);
}
db.SubmitChanges();
status = "Appointment Removed successfully";
}
if (userdata.changed.Count > 0)
{
var value = userdata.changed[0];
var filterData = db.ScheduleDatas.Where(c => c.Id == Convert.ToInt32(value.Id));
if (filterData.Count() > 0)
{
DateTime startTime = Convert.ToDateTime(value.StartTime);
DateTime endTime = Convert.ToDateTime(value.EndTime);
ScheduleData appoint = db.ScheduleDatas.Single(A => A.Id == Convert.ToInt32(value.Id));
appoint.StartTime = startTime;
appoint.EndTime = endTime;
appoint.Subject = value.Subject;
appoint.StartTimeZone = value.StartTimeZone;
appoint.EndTimeZone = value.EndTimeZone;
appoint.Recurrence = value.Recurrence;
appoint.AllDay = value.AllDay;
appoint.RecurrenceRule = value.RecurrenceRule;
}
db.SubmitChanges();
status = "Appointment Updated successfully";
}
return Json(new { status = status });
}
public class BatchData
{
public List<ScheduleData> added { get; set; }
public List<ScheduleData> changed { get; set; }
public List<ScheduleData> removed { get; set; }
} |
Thank you, both solutions work!