public async Task <IActionResult> Edit(int id, [Bind("ImageID,title,ImageName")] ImageModel imageModel) { if (id != imageModel.ImageID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(imageModel); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ImageModelExists(imageModel.ImageID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(imageModel)); }
public async Task <IActionResult> Edit(int id, [Bind("ImageID,title,ImageName,ImageFile")] ImageModel imageModel) { if (id != imageModel.ImageID) { return(NotFound()); } if (ModelState.IsValid) { try { if (imageModel.ImageFile != null) { //delete image form wwwroot/image var ImagePath = Path.Combine(_HostEnvironment.WebRootPath, "Images", imageModel.ImageName); if (System.IO.File.Exists(ImagePath)) { System.IO.File.Delete(ImagePath); //save image to wwwroot folder string wwwRootPath = _HostEnvironment.WebRootPath; string Filename = Path.GetFileNameWithoutExtension(imageModel.ImageFile.FileName); string extension = Path.GetExtension(imageModel.ImageFile.FileName); Filename = Filename + DateTime.Now.ToString("yymmssfff") + extension; imageModel.ImageName = Filename; string path = Path.Combine(wwwRootPath + "/Images/" + Filename); using (var fileStream = new FileStream(path, FileMode.Create)) { await imageModel.ImageFile.CopyToAsync(fileStream); } } } //update record _context.Update(imageModel); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ImageModelExists(imageModel.ImageID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(imageModel)); }