public ActionResult Save(Trainer trainer) { if (!ModelState.IsValid) { var viewModel = new TrainerFormViewModel { Trainer = trainer, SportTypes = _context.SportTypes.ToList(), Players = _context.Players.ToList() }; return(View("TrainerForm", viewModel)); } if (trainer.Id == 0) { _context.Trainers.Add(trainer); } else { var trainerInDb = _context.Trainers.Single(c => c.Id == trainer.Id); trainerInDb.Name = trainer.Name; trainerInDb.Birthdate = trainer.Birthdate; trainerInDb.SportTypeId = trainer.SportTypeId; trainerInDb.PlayerId = trainer.PlayerId; trainerInDb.HasLicense = trainer.HasLicense; } _context.SaveChanges(); return(RedirectToAction("Index", "Trainers")); }
public ActionResult New() { var sportTypes = _context.SportTypes.ToList(); var players = _context.Players.ToList(); var viewModel = new TrainerFormViewModel { Trainer = new Trainer(), SportTypes = sportTypes, Players = players, }; return(View("TrainerForm", viewModel)); }
public ActionResult Edit(int id) { var trainer = _context.Trainers.SingleOrDefault(c => c.Id == id); if (trainer == null) { return(HttpNotFound()); } var viewModel = new TrainerFormViewModel { Trainer = trainer, SportTypes = _context.SportTypes.ToList(), Players = _context.Players.ToList(), }; return(View("TrainerForm", viewModel)); }