//[Authorize] public IActionResult Create(EmployeeCreateViewmodel model) { //to check if validation has suceeded if (ModelState.IsValid) { //because we use this in the edit view we can refactore it to do that we select the lines of code //and then extract so it creates its own method bellow string uniqueFilename = ProcessUploadedFile(model); Employee newEmployee = new Employee { Id = model.Id, Name = model.Name, Email = model.Email, Department = model.Department, Photopath = uniqueFilename }; _employeeRepository.Add(newEmployee); return(RedirectToAction("details", new { id = newEmployee.Id })); } //else return create view return(View()); //new {id = newEmployee.Id is used to pass the new id to the details //because the id is the arguement in the path controller:home action:details //arguement:id it could be abc if the arguement/parameter on the action was abc }
public IActionResult Create(EmployeeCreateViewmodel model) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedFile(model); Employee NewEmployee = new Employee { Name = model.Name, Email = model.Email, Department = model.Department, PhotoPath = uniqueFileName }; _employeeRepository.Add(NewEmployee); return(RedirectToAction("details", new { id = NewEmployee.Id })); } return(View()); }
private string ProcessUploadedFile(EmployeeCreateViewmodel model) { string uniqueFileName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string FilePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(FilePath, FileMode.Create)) { model.Photo.CopyTo(fileStream); } } return(uniqueFileName); }
public async Task <IActionResult> Create(EmployeeCreateViewmodel model) { if (ModelState.IsValid) { var employee = new Employee { Id = model.Id, EmpNo = model.EmpNo, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, FullName = model.FullName, Gender = model.Gender, Address = model.Address, City = model.City, PostalCode = model.PostalCode, PhoneNumber = model.PhoneNumber, DOB = model.DOB, DateJoined = model.DateJoined, Designation = model.Designation, Email = model.Email, InsuranceNo = model.InsuranceNo, PaymentMethod = model.PaymentMethod, StudentLoan = model.StudentLoan, UnionMember = model.UnionMember, }; if (model.ImageUrl != null && model.ImageUrl.Length > 0) { var UploadDir = @"Images/employeeDP"; var Filename = Path.GetFileNameWithoutExtension(model.ImageUrl.FileName); var Extension = Path.GetExtension(model.ImageUrl.FileName); var WebRoot = _hostingEnvironment.WebRootPath; Filename = DateTime.UtcNow.ToString("yyyymmdd") + Filename + Extension; var path = Path.Combine(WebRoot, UploadDir, Filename); await model.ImageUrl.CopyToAsync(new FileStream(path, FileMode.Create)); employee.ImageUrl = "/" + UploadDir + "/" + Filename; } await _EmployeeService.CreateAsync(employee); return(RedirectToAction(nameof(Index))); } return(View()); }
private string ProcessUploadedFile(EmployeeCreateViewmodel model) { string uniqueFilename = null; if (model.Photo != null) { //hostingEnvironment.Webrootpath returns the wwwrootpath and then to get to the images //we have to combine the path with the images folder string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); //Guid = global unique identifier uniqueFilename = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFilename); //copy the new filenamepath to the sqlserver to save the image //we use the using to avoid certain problems with sequences like someone creating and editing the image //that the added to the web page immediately, its good practise using (var filestream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(filestream); } } return(uniqueFilename); }
public IActionResult Create() { var model = new EmployeeCreateViewmodel(); return(View(model)); }