Exemplo n.º 1
0
        public IActionResult Edit(HomeEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var employee = new Employee()
                {
                    Id         = model.Id,
                    Fullname   = model.Fullname,
                    Email      = model.Email,
                    Department = model.Department,
                    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);
                    }
                    employee.AvatarPath = fileName;
                    if (!string.IsNullOrEmpty(model.AvatarPath))
                    {
                        string delFile = Path.Combine(webHostEnvironment.WebRootPath, "images", model.AvatarPath);
                        System.IO.File.Delete(delFile);
                    }
                }

                var editEmp = employeeRespository.Edit(employee);
                if (editEmp != null)
                {
                    return(RedirectToAction("Details", new { id = editEmp.Id }));
                }
            }

            return(View());
        }