// GET /Associate/Create
        // create a new associate
        public ActionResult Create()
        {
            var associate = new AssociateCreateModel();

            SetCreateAssociateOptions(associate);

            return View(associate);
        }
        public ActionResult Create(AssociateCreateModel associate)
        {
            @ViewBag.Title = "Create Associate";

            try
            {
                // Basic validation passed.
                if (!this.ModelState.IsValid)
                {
                    this.ViewBag.Feedback = "The associate has not been created";

                    this.ModelState.AddModelError(
                        string.Empty,
                        "You have entered some invalid data, please check all highlighted fields on all tabs.");
                }
                else
                {
                    // get the username of the current user
                    string userName = this.MembershipService.GetCurrentUserName();

                    // If leading whitespace is entered in the email field this then causes an error when verifying email address
                    // as it calls GetAssociate and passes the trimmed email address so can't find it. Just trim this off now.
                    associate.Email = associate.Email.TrimStart();

                    associate.AssociateRegistrationTypeId = (byte?) AssociateRegistrationType.Agency;

                    associate.ReferralSourceId = (int) ReferralSourceId.Other;
                    associate.ReferralName = "Agency";

                    MembershipCreateStatus createStatus;

                    using (var scope = new TransactionScope(
                        TransactionScopeOption.Required,
                        new TransactionOptions
                        {
                            IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted
                        }))
                    {
                        createStatus = this.RegisterNewAssociate(associate);

                        scope.Complete();
                    }

                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        associateService.AddAssociateToAssociateRole(associate.Id);

                        var email = GetEmailModel(associate.EmailModel, associate.Id, EmailTemplate.None);

                        // confirmation url
                        var user = this.AssociateMembership.GetUser(associate.Email);

                        var confirmationGuid = user.ProviderUserKey.ToString();
                        var verifyUrl = this.GetAssociatePortalUrl() + "v/" + confirmationGuid;

                        email.Body = email.Body.Replace("CONFIRMURL", verifyUrl);

                        this.emailService.SendEmail(email);

                        return this.RedirectToAction("Index");
                    }
                }
            }
            catch (DataException ex)
            {
                this.ViewBag.Feedback = "Unexpected error.";

                ErrorSignal.FromCurrentContext().Raise(ex);
                this.ModelState.AddModelError(
                    string.Empty,
                    "Unable to save associate details. Please try again or contact your system administrator.");
            }

            SetCreateAssociateOptions(associate);

            return View(associate);
        }
        private void SetCreateAssociateOptions(AssociateCreateModel associate)
        {
            // titles
            var titles = ListItem.GetSelectListItems(
                this.associateService.GetAssociateTitleOptions(),
                arg => associate != null && associate.PersonTitleId == arg.Value);

            var selectTitleOption = new SelectListItem { Selected = true, Text = "-- Select --", Value = string.Empty };
            titles = titles.Insert(selectTitleOption, 0);

            this.ViewBag.Titles = titles;

            // agency
            var agency = ListItem.GetSelectListItems(
                this.associateService.GetAgencyOptions(),
                arg => associate.AgencyId.HasValue && associate.AgencyId == arg.Value);

            var selectAgencyOption = new SelectListItem { Selected = true, Text = "-- Select --", Value = string.Empty };
            agency = agency.Insert(selectAgencyOption, 0);
            this.ViewBag.Agency = agency;

            this.ViewBag.Title = "Create Associate";
        }