Beispiel #1
0
        public async Task <IActionResult> TrainerAdd(Trainer trainer, IFormFile formFile)
        {
            if (formFile.Length == 0 && formFile == null)
            {
                TempData.Add("message", "Image not selected");
                return(RedirectToAction("TrainerAdd"));
            }
            else
            {
                using (var stream = new MemoryStream())
                {
                    await formFile.CopyToAsync(stream);

                    trainer.Image = stream.ToArray();
                }
            }
            if (ModelState.IsValid)
            {
                _trainerService.Add(trainer);
                TempData.Add("message", "Trainer successfully added");
                return(RedirectToAction("TrainerAdd"));
            }
            else
            {
                TrainerAddViewModel trainerAddViewModel = new TrainerAddViewModel()
                {
                    Trainer = trainer
                };
                TempData.Add("message", "Trainer not added");
            }
            return(View());
        }