public JsonResult SavedEvent(string eventTitle, string startTime, string endTime, bool isAllDay, bool isFixed) { try { if (Session["student"] == null) { return Json(false); } EventModel eventModel = new EventModel { EndTime = DateTime.Parse(endTime), StartTime = DateTime.Parse(startTime), EventTitle = eventTitle, IsAllDay = isAllDay, IsFixed = isFixed, Color = isFixed ? "green" : "red" }; Guid id = Guid.Parse((string)Session["student"]); using (var context = new TobyDBContext()) { eventModel.StudentId = id; context.EventModels.Add(eventModel); context.SaveChanges(); } return Json(eventModel.Id); } catch { return Json(false); } }
public JsonResult SavedEvents(EventJsonModel jsonModel) { DateTime start, end, temp; EventModel eventModel; try { if (jsonModel.studentIds == null) { throw new Exception(); } if (DateTime.TryParse(jsonModel.startTime, out start) && DateTime.TryParse(jsonModel.endTime, out end)) { if (start > end) { temp = start; start = end; end = temp; } } else { throw new Exception(); } using (var context = new TobyDBContext()) { jsonModel.studentIds.ForEach(id => { eventModel = new EventModel { EventTitle = jsonModel.eventTile, StartTime = start, EndTime = end, StudentId = id, TeacherId = jsonModel.teacherId, WorkTypeId = jsonModel.workTypeId, Color = "red", IsFixed = false, IsAllDay = false }; context.EventModels.Add(eventModel); }); context.SaveChanges(); } return Json(true); } catch { return Json(false); } }