public ActionResult Insert()
        {
            var newEvent = new Event();

            var model = CreateEventModel(newEvent);

            return View(model);
        }
        public ActionResult Edit(Event model)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = CreateEventModel(model);
                return View(viewModel);
            }

            _unitOfWork.Events.SaveOnSubmit(model);
            _unitOfWork.Events.SubmitChanges();

            return RedirectToAction("Index");
        }
        public ActionResult Insert(Event model)
        {
            InsertEventValidation(model);

            if (!ModelState.IsValid)
            {
                var viewModel = CreateEventModel(model);
                return View(viewModel);
            }

            model.Id = Guid.NewGuid();
            _unitOfWork.Events.SaveOnSubmit(model);
            _unitOfWork.Events.SubmitChanges();

            return RedirectToAction("Index");
        }
 private EventModel CreateEventModel(Event selectedEvent)
 {
     var model = new EventModel
     {
         Event = selectedEvent,
         Speakers = _unitOfWork.Speakers.LoadAll().Select(x => new SelectListItem
         {
             Text =
                 _unitOfWork.Persons.LoadSingle(y => y.Id == x.PersonId).FirstName + " " +
                 _unitOfWork.Persons.LoadSingle(y => y.Id == x.PersonId).LastName,
             Value = x.Id.ToString(),
             Selected = selectedEvent.SpeakerIds.Contains(x.Id)
         }).OrderBy(x => x.Text),
         Locations = _unitOfWork.Locations.LoadAll().Select(x => new SelectListItem
         {
             Text = x.Name,
             Value = x.Id.ToString(),
             Selected = selectedEvent.LocationId.Equals(x.Id)
         })
     };
     return model;
 }
 private void InsertEventValidation(Event model)
 {
     if (model.FromDate <= DateTime.Now)
     {
         ModelState.AddModelError("FromDate", "Das Startdatum muss in der Zukunft liegen.");
     }
     if (model.ToDate <= DateTime.Now)
     {
         ModelState.AddModelError("ToDate", "Das Enddatum muss in der Zukunft liegen.");
     }
     if (model.FromDate > model.ToDate)
     {
         ModelState.AddModelError("FromDate", "Das Startdatum darf nicht später als das Enddatum sein.");
         ModelState.AddModelError("ToDate", "Das Enddatum darf nicht früher als das Startdatum sein.");
     }
     if (!model.SpeakerIds.Any())
     {
         ModelState.AddModelError("SpeakerIds", "Es muss mindestens ein Sprecher ausgewählt werden.");
     }
     if (model.LocationId == Guid.Empty)
     {
         ModelState.AddModelError("LocationId", "Es muss eine Location ausgewählt werden.");
     }
 }