public ActionResult Add(EventsFormViewModel viewModel) { // Date format must be valid if (viewModel.CorrectDateFormat() == false) { ModelState.AddModelError("Date", "The date format is invalid."); // Events list is not null viewModel.EventTypes = _context.EventTypes.ToList(); return(View("EventForm", viewModel)); } // Time format must be valid if (viewModel.CorrectTimeFormat() == false) { ModelState.AddModelError("Time", "The time format is invalid."); // Events list is not null viewModel.EventTypes = _context.EventTypes.ToList(); return(View("EventForm", viewModel)); } // Ensure form validation if (!ModelState.IsValid || viewModel.DateTimeAvailable() == false) { ModelState.AddModelError("Date", "The date/time is not available."); ModelState.AddModelError("Time", "The date/time is not available."); // Events list is not null viewModel.EventTypes = _context.EventTypes.ToList(); return(View("EventForm", viewModel)); } // Creat an Event object from the information from the form for the database var e = new Event { EventOwnerId = User.Identity.GetUserId(), DateTime = viewModel.GetDateTime(), EventTypeId = viewModel.EventType, EventLocation = viewModel.EventLocation, EventName = viewModel.EventName, Description = viewModel.Description }; // Add to database _context.Events.Add(e); _context.SaveChanges(); if (viewModel.Attending) { return(RedirectToAction("Attend", "Attendance", new { eventId = e.Id })); } return(RedirectToAction("MyEvents", "Events")); }
public ActionResult Edit(EventsFormViewModel viewModel) { // Date format must be valid if (viewModel.CorrectDateFormat() == false) { ModelState.AddModelError("Date", "The date format is invalid."); // Events list is not null viewModel.EventTypes = _context.EventTypes.ToList(); return(View("EventForm", viewModel)); } // Time format must be valid if (viewModel.CorrectTimeFormat() == false) { ModelState.AddModelError("Time", "The time format is invalid."); // Events list is not null viewModel.EventTypes = _context.EventTypes.ToList(); return(View("EventForm", viewModel)); } // Ensure form validation if (!ModelState.IsValid || viewModel.DateTimeAvailable() == false) { ModelState.AddModelError("Date", "The date/time is not available."); ModelState.AddModelError("Time", "The date/time is not available."); // Events list is not null viewModel.EventTypes = _context.EventTypes.ToList(); return(View("EventForm", viewModel)); } var userId = User.Identity.GetUserId(); // Get the event to change var eVent = _context.Events.Single(e => e.Id == viewModel.Id && e.EventOwnerId == userId); // Event properties are updated eVent.EventName = viewModel.EventName; eVent.EventLocation = viewModel.EventLocation; eVent.DateTime = viewModel.GetDateTime(); eVent.Description = viewModel.Description; eVent.EventTypeId = viewModel.EventType; // Update database _context.SaveChanges(); return(RedirectToAction("MyEvents", "Events")); }