Barrosy
Barrosy

Reputation: 1457

How to edit registration part in ASP.NET MVC?

I am working on the registration part of my project. Once a user wants to register an account, I want the user to also fill in their address, city, postal code, first name, last name and some other stuff (descibed in model below).

How can I approach this?

I tried changing the following code:

~/Controllers/AccountController.cs:

//
// GET: /Account/Register

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

//
// POST: /Account/Register

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
    if (ModelState.IsValid)
    {
        // Attempt to register the user
        try
        {
            WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
            WebSecurity.Login(model.UserName, model.Password);
            return RedirectToAction("Index", "Home");
        }
        catch (MembershipCreateUserException e)
        {
            ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
        }
    }

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

I noticed there is a part of code within the post method public ActionResult Register(RegisterModel model) {} which saves only the username and password in tables called UserProfile and webpages_Membership, which looks like this:

try
{
    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
    WebSecurity.Login(model.UserName, model.Password);
    return RedirectToAction("Index", "Home");
}

I am not supposed to change the WebSecurity.CreateUserAndAccount() function, since this only takes a couple arguments, a username and a password and something other than that.

I did however change my Model and View to the following:

~/Views/Account/Register.cshtml:

@model Rent_a_Car_MVC.Models.RegisterModel
@{
    ViewBag.Title = "Registreren";
}

<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    <h4>Maak een account aan.</h4>
    <hr />
    @Html.ValidationSummary()

    <div class="form-group">
        @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.FirstName, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.LastName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.LastName, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.Address, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.Address, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.PostalCode, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.PostalCode, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.City, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.City, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" class="btn btn-default" value="Register" />
        </div>
    </div>
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

~/Models/AccountModel.cs:

public class RegisterModel
{
    [Required]
    [Display(Name = "Gebruikersnaam")]
    public string UserName { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "{0} moet minstens {2} letters bevatten.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Wachtwoord")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Bevestig wactwoord")]
    [Compare("Password", ErrorMessage = "Wachtwoord en bevestigings wachtwoord komen niet overeen.")]
    public string ConfirmPassword { get; set; }
    [Required]
    [Display(Name = "Voornaam")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name = "Achternaam")]
    public string LastName { get; set; }
    [Required]
    [Display(Name = "Adres")]
    public string Address { get; set; }
    [Required]
    [Display(Name = "Postcode")]
    public string PostalCode { get; set; }
    [Required]
    [Display(Name = "Woonplaats")]
    public string City { get; set; }
}

Am I doing this correctly? Should I only have to change the controller and if so, how would I need to approach this?

Upvotes: 0

Views: 2585

Answers (1)

Ms. Whitney
Ms. Whitney

Reputation: 37

This link gives step-by-step instructions:

Below, I have included a brief summary of the steps you will need to complete as outlined in the article:

  1. Run the application and register a user
  2. Enable Entity Framework Migrations
  3. Add properties for the Profile information (address, city, postal code, etc.) that you want to store.
  4. Add-Migrations to modify the database
  5. Modify the application to add fields for your new properties
  6. Update the Register Action in AccountController to store the new properties as well for the User
  7. Run the application and register a user again. You will see test boxes to input values for your newly added properties. 8.Display the profile infromation on the page (optional step)

I believe this is what you are trying to do. Let me know if I have misunderstood.

Upvotes: 3

Related Questions