Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 mvc 3 i have a working register form and a working login form

but... how do i add more fields to the registration form? i know how to add them my model and to the page.. but how do i make the membership keep this new data ill get for the user? will i have to make the columns in the database by myself? or does membership knows how to create them automaticly somehow?

i only want 3 more fields for the registration...

thanks

like image 268
guy schaller Avatar asked Dec 03 '11 16:12

guy schaller


People also ask

How to create a registration form in ASP NET Core?

Choose Web Application (Model-View-Controller) template and click on create which will create your first ASP.Net Core Application. Now under Models folder create class ApplicationUser. This class will inherit from IdentityUser class. Now define your fields which you want to “Add” in your registration form.

How to add fields in registration form using identityuser class?

This class will inherit from IdentityUser class. Now define your fields which you want to “Add” in your registration form. Now open ApplicationDbContext.cs class which location under Data folder.

How to add fields to registration form in Laravel?

Now define your fields which you want to “Add” in your registration form. Now open ApplicationDbContext.cs class which location under Data folder. Modify class as below, Now open startup.cs class and “Add” following code or configure ApplicationUser class. Now create a folder called ViewModel and “Add” a class with name RegisterViewModel.

How do I register a new user in a form?

Click on Register.cs in the Solution Explorer and on the form that displays add two buttons, three text boxes, and three labels. The first button will be a register button to save input data, and the second one will be an exit button that will close the Register form. The first text box will allow the input of the name of the user.


2 Answers

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

        <div class="editor-label">
            @Html.LabelFor(m => m.Nickname )
        </div>
        <div class="editor-field">
            @Html.PasswordFor(m => m.Nickname )
            @Html.ValidationMessageFor(m => m.Nickname )
        </div>

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

<profile>
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
  <properties>
    <add name="Nickname" defaultValue="False" type="System.String" />
  </properties>
</profile>

Then in your code - you can do

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

to get/set your properties in Profile

like image 90
Paweł Staniec Avatar answered Nov 09 '22 13:11

Paweł Staniec


I suggest you this solution.

  1. Create a table called UserDetails
  2. Add a field that point to the UserId

You're done.

Now you need some class to retrieve thoses fields based on the UserId. You could also implement your own MembershipProvider and add a method like GetUserDetail() which return an object containing your extra fields.

This is based on an official ASP.NET article : http://www.asp.net/web-forms/tutorials/security/membership/storing-additional-user-information-cs

like image 45
Rushino Avatar answered Nov 09 '22 11:11

Rushino