public ActionResult Register(RegisterModel model) { RecaptchaVerificationHelper recaptchaHelper = this.GetRecaptchaVerificationHelper(); model.States = _serviceLayer.GetStates().Select(x => new SelectListItem { Text = x.Abbr, Value = x.Id.ToString() }); //model.BusinessTypes = _serviceLayer.GetBusinessTypes().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); if (String.IsNullOrEmpty(recaptchaHelper.Response)) { ModelState.AddModelError("", "Captcha answer cannot be empty."); return View(model); } RecaptchaVerificationResult recaptchaResult = recaptchaHelper.VerifyRecaptchaResponse(); if (recaptchaResult != RecaptchaVerificationResult.Success) { ModelState.AddModelError("", "Incorrect captcha answer."); } int cpId; if (ModelState.IsValid) { // Attempt to register the user try { CompanyProfile cp = new CompanyProfile { Address1 = model.Address1, Address2 = model.Address2, BusinessType = model.BusinessType, City = model.City, CompanyName = model.CompanyName, OperatingDistance = model.OperatingDistance, Phone = Util.ConvertPhoneForStorage(model.Phone), PostalCode = model.PostalCode, Published = true, StateId = model.StateId }; GeoLocator locator = new GeoLocator(); string state = _serviceLayer.GetStates().Where(x => x.Id == model.StateId).FirstOrDefault().Abbr; if (model.Address1 == null || model.Address1 == string.Empty) { cp.GeoLocation = locator.GetFromCityStateZip(model.City, state, model.PostalCode); } else { cp.GeoLocation = locator.GetFromAddress(model.Address1, model.City, state, model.PostalCode); } // if we can create the company, create the user if (_serviceLayer.CreateCompany(cp)) { cpId = cp.Id; string confirmationToken = _security.CreateUserAndAccount( model.Email, model.Password, new { FirstName = model.FirstName, LastName = model.LastName, CompanyId = cp.Id }, true); _security.AddUserToRole(model.Email, "Manager"); //var businesstypes = _serviceLayer.GetBusinessTypes(); switch (model.BusinessType) { case BusinessType.GeneralContractor: _security.AddUserToRole(model.Email, "general_contractor"); break; case BusinessType.SubContractor: _security.AddUserToRole(model.Email, "subcontractor"); break; case BusinessType.Architect: _security.AddUserToRole(model.Email, "architect"); break; case BusinessType.Engineer: _security.AddUserToRole(model.Email, "engineer"); break; case BusinessType.Owner: _security.AddUserToRole(model.Email, "owner_client"); break; case BusinessType.MaterialsVendor: _security.AddUserToRole(model.Email, "materials_vendor"); break; case BusinessType.MaterialsMfg: _security.AddUserToRole(model.Email, "materials_manufacturer"); break; case BusinessType.Consultant: _security.AddUserToRole(model.Email, "consultant"); break; }; _emailer.SendConfirmationMail(model.FirstName, model.Email, confirmationToken); return RedirectToAction("RegisterStepTwo", "Account"); } } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } catch (Exception ex) { ModelState.AddModelError("Exception", ex.Message); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult Register() { if (_security.IsAuthenticated) { return RedirectToRoute("Default", new { controller = "Accout", action = "Manage" }); } RegisterModel viewModel = new RegisterModel(); viewModel.States = _serviceLayer.GetStates().Select(x => new SelectListItem { Text = x.Abbr, Value = x.Id.ToString() }); //viewModel.BusinessTypes = _serviceLayer.GetBusinessTypes().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }); return View("Register", viewModel); }