public IActionResult Edit(HomeEditViewModel model) { if (ModelState.IsValid) { var flower = new Flower() { Name = model.Name, TypeF = model.TypeF, Id = model.Id, AvatarPath = model.AvatarPath }; var fileName = string.Empty; if (model.Avatar != null) { string uploadFolder = Path.Combine(webHostEnvironment.WebRootPath, "images"); fileName = $"{Guid.NewGuid()}_{model.Avatar.FileName}"; var filePath = Path.Combine(uploadFolder, fileName); using (var fs = new FileStream(filePath, FileMode.Create)) { model.Avatar.CopyTo(fs); } flower.AvatarPath = fileName; if (!string.IsNullOrEmpty(model.AvatarPath)) { string delFile = Path.Combine(webHostEnvironment.WebRootPath, "images", model.AvatarPath); System.IO.File.Delete(delFile); } } var editEmp = flowerRepository.Edit(flower); if (editEmp != null) { return(RedirectToAction("Details", new { id = flower.Id })); } } return(View()); }