public async Task <IActionResult> Create([Bind("Id,About,Upload,Title,Desc")] WelcomeClinic welcomeClinic) { if (welcomeClinic.Upload == null) { ModelState.AddModelError("Uploads", "The Photo field is required."); } if (ModelState.IsValid) { var fileName = _fileManager.Upload(welcomeClinic.Upload, "wwwroot/uploads/main-slider"); welcomeClinic.Photo = fileName; _context.Add(welcomeClinic); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(welcomeClinic)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,About,Title,Desc,Photo,Upload")] WelcomeClinic welcomeClinic) { if (id != welcomeClinic.Id) { return(NotFound()); } if (ModelState.IsValid) { try { if (welcomeClinic.Upload != null) { var oldFile = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads/main-slider", welcomeClinic.Photo); _fileManager.Delete(oldFile); var fileName = _fileManager.Upload(welcomeClinic.Upload, "wwwroot/uploads/main-slider"); welcomeClinic.Photo = fileName; } _context.Update(welcomeClinic); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!WelcomeClinicExists(welcomeClinic.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(welcomeClinic)); }