private string UploadAuthorPhoto(AuthorEditViewModelcs model) { string uniqueName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "images/authorImages/"); uniqueName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(fileStream); } } return(uniqueName); }
public IActionResult Edit(int?id) { var model = new AuthorEditViewModelcs(); if (id.HasValue) { model.Author = authorService.GetAuthorById(id.Value); if (model.Author == null) { return(View("NotFound")); } } else { model.Author = new Author(); } return(View(model)); }
public IActionResult Edit(AuthorEditViewModelcs model) { if (ModelState.IsValid) { if (model.Author.Id == 0) { if (model.Photo != null) { if (model.Author.AuthorPhoto != null) { string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images/authorImages/", model.Author.AuthorPhoto); System.IO.File.Delete(filePath); } model.Author.AuthorPhoto = UploadAuthorPhoto(model); } model.Author = authorService.CreateAuthor(model.Author); TempData["Message"] = "New Author Created!"; } else { if (model.Photo != null) { if (model.Author.AuthorPhoto != null) { string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images/authorImages/", model.Author.AuthorPhoto); System.IO.File.Delete(filePath); } model.Author.AuthorPhoto = UploadAuthorPhoto(model); } model.Author = authorService.UpdateAuthor(model.Author); TempData["Message"] = "Author updated!"; } authorService.Commit(); return(RedirectToAction("Details", new { id = model.Author.Id })); } return(View(model)); }