Example #1
0
        public bool UpdateEmployee(EmployeeUserDescriptionViewModel updateData)
        {
            Employee updateEmp  = new Employee();
            User     updateUser = new User();

            try
            {
                if (updateData != null)
                {
                    //gather password from user to be added to the updated employee
                    var userPassword = from user in db.Users
                                       where user.User_Id == updateData.userId
                                       select user.Password;

                    updateEmp.FirstName        = updateData.employeeFirstName;
                    updateEmp.LastName         = updateData.employeeLastName;
                    updateEmp.DOB              = updateData.employeeDOB;
                    updateEmp.Id               = updateData.userId;
                    updateEmp.User_id          = updateData.userId;
                    updateUser.Username        = updateData.userName;
                    updateUser.Password        = userPassword.FirstOrDefault();
                    updateUser.User_Id         = updateData.userId;
                    db.Entry(updateUser).State = EntityState.Modified;
                    db.Entry(updateEmp).State  = EntityState.Modified;
                    db.SaveChanges();
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(false);
        }
        public ActionResult Edit(int id, DateTime dob, string firstName, string lastName,
                                 string userName)
        {
            EmployeeUserDescriptionViewModel data = new EmployeeUserDescriptionViewModel();

            data.employeeDOB       = dob;
            data.employeeFirstName = firstName.Trim();
            data.employeeLastName  = lastName.Trim();
            data.userName          = userName.Trim();
            data.userId            = id;

            return(View(data));
        }
        public ActionResult EditEmployee(EmployeeUserDescriptionViewModel model)
        {
            AccountManager manager = new AccountManager();

            try
            {
                if (ModelState.IsValid)
                {
                    if (manager.UpdateEmployee(model))
                    {
                        Session["Update"] = "Employee Successfully modified";
                    }
                    return(RedirectToAction("Index", "ListAccounts"));
                }
                Session["Update"] = "Could not modify employee, please contact support.";
                return(RedirectToAction("Index", "ListAccounts", ViewBag));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            return(View(model));
        }