Exemple #1
0
        public ActionResult Create([Bind(Include = "ID,Name,Email,Cel,Position,DutyDays,WorkShift")] EmployeeModel employeeModel, HttpPostedFileBase Image)
        {
            if (ModelState.IsValid)
            {
                string userId  = User.Identity.GetUserId();
                var    userAdm = db.Users.Find(userId);

                MembershipHandler membership = new MembershipHandler();
                var user = new ApplicationUser {
                    UserName = employeeModel.Email, Email = employeeModel.Email, Condo_ID = userAdm.Condo_ID
                };

                membership.CreateUser(user, Request.Form["pass"]);
                membership.SetRoleEmpregado(user.Id);

                employeeModel.User = db.Users.Find(user.Id);

                db.EmployeeModels.Add(employeeModel);
                db.SaveChanges();

                if (Image != null)
                {
                    StorageHandler.UploadImage(employeeModel.ID.ToString(), Image, "employee_");
                }

                return(RedirectToAction("Index"));
            }

            return(View(employeeModel));
        }
        public ActionResult Create([Bind(Include = "ID,CPF,Name,Birthdate,Cel,Phone,Email,EmailOthers,Residents,Pets,Cars,Visitors")] UserModel userModel, HttpPostedFileBase Image, int idCondo)
        {
            //int idCondo = 0;

            if (ModelState.IsValid)
            {
                MembershipHandler membership = new MembershipHandler();

                var user = new ApplicationUser {
                    UserName = userModel.Email, Email = userModel.Email, Condo_ID = idCondo
                };
                membership.CreateUser(user, Request.Form["pass"]);

                if (TempData["adm"] != null)
                {
                    if (TempData["adm"].ToString() == "S")
                    {
                        membership.SetRoleSindico(user.Id);
                        TempData.Clear();
                    }
                }
                else
                {
                    membership.SetRoleCondomino(user.Id);
                }

                userModel.User = db.Users.FirstOrDefault(x => x.Email == userModel.Email);


                var towerId = Convert.ToInt32(Request.Form["TowerId"]);
                var tower   = db.TowerModels.FirstOrDefault(x => x.ID == towerId);

                UnitModel unit = new UnitModel();
                unit.Name     = String.Format(Request.Form["Unit"]);
                unit.Tower_ID = tower.ID;
                unit.Tower    = tower;

                db.UnitModels.Add(unit);
                db.SaveChanges();

                userModel.Unit = unit;

                db.UserModels.Add(userModel);
                db.SaveChanges();

                if (Image != null)
                {
                    StorageHandler.UploadImage(userModel.ID.ToString(), Image, "user_");
                }

                membership.Login(user, HttpContext);
                return(RedirectToAction("Index", "Home", ""));
            }

            ViewBag.TowerId = new SelectList(db.TowerModels.Include(c => c.Condo).Where(x => x.Condo.ID == idCondo).ToList(), "Id", "Name");
            ViewBag.CondoId = idCondo;

            return(View(userModel));
        }