public IActionResult Edit(int id) { var memory = service.GetMemoryById(id); MemoryEditVM memoryViewModel = service.GetMemoryEditVM(memory); return(View(memoryViewModel)); }
public IActionResult Edit(MemoryEditVM memoryVM, int id) { if (!ModelState.IsValid) { return(View()); } service.EditMemory(memoryVM, id); return(RedirectToAction(nameof(Index))); }
internal void EditMemory(MemoryEditVM viewModel, int id) { if (viewModel.ImageToUpload != null) { string filePath = Path.Combine(webHostEnv.WebRootPath, "Uploads", viewModel.ImageToUpload.FileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { viewModel.ImageToUpload.CopyTo(fileStream); } } var memoryToUpdate = context.Memories .Find(id); memoryToUpdate.MemoryHolder = viewModel.MemoryHolder; memoryToUpdate.PeopleInMemory = viewModel.PeopleInMemory; memoryToUpdate.MemoryTitle = viewModel.MemoryTitle; memoryToUpdate.WhenInWords = viewModel.WhenInWords; memoryToUpdate.Description = viewModel.Description; memoryToUpdate.ImagePath = viewModel.ImageToUpload == null ? memoryToUpdate.ImagePath : viewModel.ImageToUpload.FileName; memoryToUpdate.HasImage = viewModel.ImageToUpload?.FileName.Length > 0; context.SaveChanges(); }