Esempio n. 1
0
        public ActionResult Create([Bind(Include = "EmployeeId,FirstName,MiddleName,LastName,EMail,EMailPassword,PersonalID,HireDate,Position,Department")] Employee employee, int?Sup, int?RoleId, int Hq)
        {
            if (ModelState.IsValid)
            {
                //ViewBag.Sup = new SelectList(db.MyEmployee, "EmployeeId", "FirstName");
                EmployeeDAL ed = new EmployeeDAL();

                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        //create employee
                        if (Sup != null)
                        {
                            int      supId = Sup.Value;
                            Employee sup   = ed.GetEmployeeById(supId);// db.MyEmployee.Find(Sup);
                            employee.SuperiorEmployee = sup;
                        }
                        if (Hq != 0)
                        {
                            employee.Headquarter = ed.GetAddressById(Hq);
                        }
                        employee.EndDate = new DateTime(1800, 1, 1);
                        //db.MyEmployee.Add(employee);
                        //db.SaveChanges();
                        ed.AddEmployeeAndSaveChanges(employee);

                        ed.CreateUserAndAddToRole(employee);

                        /*
                         * //create user for employee
                         * var user = new ApplicationUser();
                         * user.UserName = employee.FirstName + employee.LastName;
                         * user.MyEmployee = employee;
                         * var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
                         * var adminresult = UserManager.Create(user, "password");
                         * //db.SaveChanges();
                         * ed.saveChanges();
                         *
                         * //Add User to Role 'Employee'
                         * if (adminresult.Succeeded)
                         * {
                         *  var result = UserManager.AddToRole(user.Id, "Employee");
                         *  //db.SaveChanges();
                         *  ed.saveChanges();
                         * }*/
                        trans.Commit();
                    }
                    catch (Exception e)
                    {
                        trans.Rollback();
                    }
                }
                return(RedirectToAction("Index"));
            }

            return(View(employee));
        }