public ActionResult Create(EmployerViewModel viewModel)
        {
            try
            {
                var validationResult = _PayrollBureauBusinessService.EmployerAlreadyExists(viewModel.Employer.Name, viewModel.Email, null);

                if (!validationResult.Succeeded)
                {
                    foreach (var error in validationResult.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                    return(View(viewModel));
                }

                //create employeruser and role
                var user = new ApplicationUser
                {
                    UserName = viewModel.Email,
                    Email    = viewModel.Email,
                };

                var roleId = RoleManager.Roles.FirstOrDefault(r => r.Name == "Employer").Id;
                user.Roles.Add(new IdentityUserRole {
                    UserId = user.Id, RoleId = roleId
                });
                var result = UserManager.Create(user, "Inland12!");
                if (!validationResult.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
                //create employer
                var userId = User.Identity.GetUserId();
                viewModel.Employer.BureauId       = viewModel.BureauId;
                viewModel.Employer.AspnetUserId   = user.Id;
                viewModel.Employer.CreatedDateUtc = DateTime.UtcNow;
                viewModel.Employer.CreatedBy      = userId;
                var employer = _payrollBureauBusinessService.CreateEmployer(viewModel.Employer);
                if (employer.Succeeded)
                {
                    return(RedirectToAction("Index", "Employer"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
            }
            return(RedirectToAction("Index", "Employer"));
        }
示例#2
0
        public ActionResult Create(EmployerViewModel viewModel)
        {
            try
            {
                //create Bureau
                var userId = User.Identity.GetUserId();
                viewModel.Employer.BureauId  = viewModel.BureauId;
                viewModel.Employer.CreatedBy = userId;
                var employer = _payrollBureauBusinessService.CreateEmployer(viewModel.Employer);
                if (!employer.Succeeded)
                {
                    foreach (var error in employer.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                    return(View(viewModel));
                }

                //create employeruser and role
                var user = new ApplicationUser
                {
                    UserName = viewModel.Email,
                    Email    = viewModel.Email,
                };

                var roleId = RoleManager.Roles.FirstOrDefault(r => r.Name == "Employer").Id;
                user.Roles.Add(new IdentityUserRole {
                    UserId = user.Id, RoleId = roleId
                });
                var result = UserManager.Create(user, "Inland12!");
                if (!result.Succeeded)
                {
                    foreach (var error in result.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }

                //create AspNetUser Employer
                _payrollBureauBusinessService.CreateAspNetUserEmployer(employer.Entity.EmployerId, user.Id);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
            }
            return(RedirectToAction("Index", "Employer", new { bureauId = viewModel.BureauId }));
        }