//TRAINERS //VIEW public IActionResult Trainers() { AdminTrainersViewModel model = new AdminTrainersViewModel() { trainers = _context.trainers.Include(t => t.gym).ToList(), gyms = _context.gyms.ToList() }; return(View(model)); }
public async Task <IActionResult> AddTrainer(AdminTrainersViewModel model) { if (ModelState.IsValid) { Trainer trainer = new Trainer { title = model.trainer.title, fullName = model.trainer.fullName, email = model.trainer.email, aboutMe = model.trainer.aboutMe, gym = _context.gyms.Find(model.trainer.gym.gymID), Qualifications = model.trainer.Qualifications, Speciality = model.trainer.Speciality, image = model.trainer.Upload.FileName }; var saving = Path.Combine(_webHost.WebRootPath, "image/Trainers", model.trainer.Upload.FileName); string imgext = Path.GetExtension(model.trainer.Upload.FileName); if (imgext == ".jpg" || imgext == ".png") { using (var uploading = new FileStream(saving, FileMode.Create)) { await model.trainer.Upload.CopyToAsync(uploading); } } _context.trainers.Add(trainer); _context.SaveChanges(); TempData["Alert"] = "New Trainer added!"; return(RedirectToAction("Trainers", "Trainer", new { area = "Admin" })); } else { var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)); TempData["Alert"] = message; AdminTrainersViewModel returnModel = new AdminTrainersViewModel() { trainers = _context.trainers.Include(t => t.gym).ToList(), gyms = _context.gyms.ToList() }; return(View("Trainers", returnModel)); } }
public async Task <IActionResult> EditTrainer(AdminTrainersViewModel model) { if (ModelState.IsValid) { Trainer trainer = _context.trainers.Find(model.trainer.ID); trainer.fullName = model.trainer.fullName; trainer.title = model.trainer.title; trainer.gym = _context.gyms.Find(model.trainer.gym.gymID); trainer.aboutMe = model.trainer.aboutMe; trainer.email = model.trainer.email; trainer.Qualifications = model.trainer.Qualifications; trainer.Speciality = model.trainer.Speciality; if (!String.IsNullOrEmpty(model.trainer.Upload.FileName)) { trainer.image = model.trainer.Upload.FileName; var saving = Path.Combine(_webHost.WebRootPath, "image/Trainers", model.trainer.Upload.FileName); string imgext = Path.GetExtension(model.trainer.Upload.FileName); if (imgext == ".jpg" || imgext == ".png") { using (var uploading = new FileStream(saving, FileMode.Create)) { await model.trainer.Upload.CopyToAsync(uploading); } } } _context.SaveChanges(); TempData["Alert"] = "Trainer Updated!"; return(RedirectToAction("Trainers", "Trainer", new { area = "Admin" })); } else { var message = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage)); TempData["Alert"] = message; return(Content(message)); } }