public async Task <IActionResult> Edit(int id, EmployeeAddEditViewModel viewmodel) { if (id != viewmodel.Employee.Id) { return(NotFound()); } if (!ModelState.IsValid) { List <Department> allDepartments = await GetAllDepartments(); viewmodel.AllDepartments = allDepartments; return(View(viewmodel)); } Employee employee = viewmodel.Employee; using (IDbConnection conn = Connection) { string sql = $@"UPDATE Employee SET FirstName = '{employee.FirstName}', LastName = '{employee.LastName}', IsSupervisor = {(employee.IsSupervisor ? 1 : 0)}, DepartmentId = {employee.DepartmentId} WHERE id = {id}"; await conn.ExecuteAsync(sql); return(RedirectToAction(nameof(Index))); } }
public async Task <IActionResult> Create(EmployeeAddEditViewModel viewmodel) { if (!ModelState.IsValid) { List <Department> allDepartments = await GetAllDepartments(); viewmodel.AllDepartments = allDepartments; return(View(viewmodel)); } Employee employee = viewmodel.Employee; using (IDbConnection conn = Connection) { string sql = $@"INSERT INTO Employee ( FirstName, LastName, IsSupervisor, DepartmentId ) VALUES ( '{employee.FirstName}', '{employee.LastName}', {(employee.IsSupervisor ? 1 : 0)}, {employee.DepartmentId} );"; await conn.ExecuteAsync(sql); return(RedirectToAction(nameof(Index))); } }
/** * Purpose: Define Create method that Insert the data for Employee table in the database. * Author: Streator ward * Method: Create() - When User clicks Create link on Employee Page then a page opens with blank form to creat new employee.", */ public async Task <IActionResult> Create() { List <Department> allDepartments = await GetAllDepartments(); EmployeeAddEditViewModel viewmodel = new EmployeeAddEditViewModel { AllDepartments = allDepartments }; return(View(viewmodel)); }
/** * Purpose: Define Edit method that interact with the Employee table in the database, and Edit the data for existing employee. * Author: Aron Keen * Method: Edit([FromRoute]int id) - When a user click the Edit Link on Employee page then a a page open with a form to edit existing employee. */ public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } List <Department> allDepartments = await GetAllDepartments(); Employee employee = await GetById(id.Value); if (employee == null) { return(NotFound()); } EmployeeAddEditViewModel viewmodel = new EmployeeAddEditViewModel { Employee = employee, AllDepartments = allDepartments }; return(View(viewmodel)); }