JadeSerys
JadeSerys

Reputation: 127

MVC 4 add email address to registration

I am using the default registration for an MVC 4 project. I have added Email to the RegisterModel and the UserProfile, I can't figure out how to get it to save the email to the database on registration. Any help would be appreciated.

 [AllowAnonymous]
    public ActionResult Register()
    {
        return View();
    }

    //
    // POST: /Account/Register
    private DefaultDBContext db = new DefaultDBContext();

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            try
            {
                string confirmationToken = WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                Roles.AddUserToRoles(model.UserName, new[] { "Member" });

                WebSecurity.Login(model.UserName, model.Password);
                //collection.UserID = (int)WebSecurity.CurrentUserId;
                //db.Collections.Add(collection);
                //db.SaveChanges();
                Session["MyMenu"] = null;
                return RedirectToAction("Dashboard", "Home");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

Upvotes: 0

Views: 107

Answers (1)

drneel
drneel

Reputation: 2907

Assuming that your database table is up to date, you can do the following:

WebSecurity.CreateUserAndAccount(
         model.UserName, 
         model.Password, 
         new { Email = model.Email });

It uses the following overload of the CreateUserAndAccount() method; with each column of the object that is passed in for the propertyValues parameter being associated with a column in the table.

    //
    // Summary:
    //     Creates a new user profile entry and a new membership account.
    //
    // Parameters:
    //   userName:
    //     The user name.
    //
    //   password:
    //     The password for the user.
    //
    //   propertyValues:
    //     (Optional) A dictionary that contains additional user attributes. The default
    //     is null.
    //
    //   requireConfirmationToken:
    //     (Optional) true to specify that the user account must be confirmed; otherwise,
    //     false. The default is false.
    //
    // Returns:
    //     A token that can be sent to the user to confirm the user account.
    //
    // Exceptions:
    //   System.InvalidOperationException:
    //     The WebMatrix.WebData.SimpleMembershipProvider.Initialize(System.String,System.Collections.Specialized.NameValueCollection)
    //     method was not called.-or-The Overload:WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection
    //     method was not called.-or-The WebMatrix.WebData.SimpleMembershipProvider
    //     membership provider is not registered in the configuration of your site.
    //     For more information, contact your site's system administrator.
    public static string CreateUserAndAccount(string userName, string password, object propertyValues = null, bool requireConfirmationToken = false);

Upvotes: 1

Related Questions