public async Task <IActionResult> Edit(int?id, string[] selectedTrainingPrograms, EmployeeEdit viewModel)
        {
            if (id == null)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(viewModel.Employee);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EmployeeExists(viewModel.Employee.EmployeeID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            var employeeToUpdate = await _context.Employee
                                   .Include(e => e.Department)
                                   .Include("EmployeeComputers.Computer")
                                   .Include("EmployeeTrainingPrograms.TrainingProgram")
                                   .SingleOrDefaultAsync(m => m.EmployeeID == id);

            // Calls method to update current Employee training programs -- takes argument of selectedTrainingPrograms(checked in the view) and the curent employee
            UpdateEmployeeTrainingPrograms(selectedTrainingPrograms, employeeToUpdate);
            // Calls method to update current Emloyee Computer -- takes an argument of The selected ComputerID and the cuurent Employee
            UpdateEmployeeComputer(viewModel.SelectedComputerID, employeeToUpdate);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException /* ex */)
            {
                //log the error (uncomment ex variable name and write a log.)
                ModelState.AddModelError("", "unable to save changes. " +
                                         "try again, and if the problem persists, " +
                                         "see your system administrator.");
            }
            return(RedirectToAction("Index"));
        }
        public async Task <IActionResult> Edit(int id, [Bind("DepartmentID,Name")] Department department)
        {
            if (id != department.DepartmentID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(department);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DepartmentExists(department.DepartmentID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(department));
        }
Exemple #3
0
        public async Task <IActionResult> Edit(int id, [Bind("TrainingProgramID,Name,Description,StartDate,EndDate,MaxAttendees")] TrainingProgram trainingProgram)
        {
            if (id != trainingProgram.TrainingProgramID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(trainingProgram);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TrainingProgramExists(trainingProgram.TrainingProgramID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(trainingProgram));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ComputerID,Make,Manufacturer,DatePurchased")] Computer computer)
        {
            if (id != computer.ComputerID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(computer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ComputerExists(computer.ComputerID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(computer));
        }