public ViewResult Edit(int id) { Employee employee = _employeeRepository.GetEmployee(id); EmpEditViewModel empEditViewModel = new EmpEditViewModel() { Id = employee.Id, Name = employee.Name, Email = employee.Email, Department = employee.Department, ExistingPhotoPath = employee.PhotoPath }; return(View(empEditViewModel)); }
//public RedirectToActionResult Create(Employee emp) public ActionResult Edit(EmpEditViewModel emp) { // get the target emp if (ModelState.IsValid) { string uniqueFilename = null; if (emp.Photos != null && emp.Photos.Count > 0) { foreach (IFormFile photo in emp.Photos) { string uploadFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images"); uniqueFilename = Guid.NewGuid().ToString() + "_" + photo.FileName; string filePath = Path.Combine(uploadFolder, uniqueFilename); photo.CopyTo(new FileStream(filePath, FileMode.Create)); } } try { Employee targetEmp = _employeeRepository.GetEmployee(emp.Id); targetEmp.Name = emp.Name; targetEmp.Email = emp.Email; targetEmp.Department = emp.Department; if (uniqueFilename != null) { // remove old image.. if (emp.Photos != null) { string OldImagePath = Path.Combine(_hostingEnvironment.WebRootPath, "images", emp.ExistingPhotoPath); // Delete file System.IO.File.Delete(OldImagePath); } targetEmp.PhotoPath = uniqueFilename; } _employeeRepository.Update(targetEmp); } catch (Exception e) { Response.Redirect("error.html"); } return(RedirectToAction("details", new { id = emp.Id })); } else { //return RedirectToAction("create", emp); return(View()); } }