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