adding more fields to registration form using membership on MySql and MVC 3

前端 未结 3 591
萌比男神i
萌比男神i 2020-12-30 13:55

i started a site based on asp.net MVC 3 and MySql i got the membership to work with the MySQL .NET connector so with the default application you get with a new project of mv

3条回答
  •  萌比男神i
    2020-12-30 14:26

    take a look at your AccountModels.cs file. It contains

    public class RegisterModel
    { 
       // User name, Email Adress, Password, Password confirmation already there
    
       // you can add something like below
        [Required]
        [Display(Name = "Nickname")]
        public string Nickname { get; set; }
    }
    

    Once you have a new property in your model you need to update the view. In the Views > Account > Register.cshtml you should add

            
    @Html.LabelFor(m => m.Nickname )
    @Html.PasswordFor(m => m.Nickname ) @Html.ValidationMessageFor(m => m.Nickname )

    When you're done with that you need to update the registration logic to use your new property. Go to AccountController and find

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);
    
                if (createStatus == MembershipCreateStatus.Success)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                  //
                  // this would be a good place for you to put your code to do something with model.Nickname
                  //                    
                  return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }
    
            // If we got this far, something failed, redisplay form
            return View(model);
        }
    

    If you want to persist that information to Users ASP.NET Profile, you need this in Web.config

    
      
        
        
      
      
        
      
    
    

    Then in your code - you can do

    var userProfile = ProfileBase.Create(model.UserName);
    

    to get/set your properties in Profile

提交回复
热议问题