tags:

views:

89

answers:

3

I've been playing around with the default MVC 1 site, adding some fields to the registration section that are used to create a secondary object in the database at sign up time.

I've created the view model including the username, email, password and passwordConfirm properties, along with a few others. I then added the type to the inherits attribute on the register page, and in the controller, added a new instance of the model to the ViewData.Model property.

The problem is, when i POST the model back to the server, the passwordConfirm property is always null, no matter what i enter. The password property, is filled in correctly, as are all the other properties.

Is there something special that has to be done to get the passwordConfirm value to get passed?

Here is the relevant code:

RegistrationViewModel.cs:

public class RegistrationViewModel
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string PasswordConfirm { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string HomePhone { get; set; }
    public string CellPhone { get; set; }
    public string OtherPhone { get; set; }
    public string Twitter { get; set; }
    public string Bio { get; set; }
    public string DOB { get; set; }
}

AccountController.cs:

public ActionResult Register()
    {
        ViewData.Model = new RegistrationViewModel();
        ViewData["PasswordLength"] = MembershipService.MinPasswordLength;

        return View();
    }

Register.aspx:

<% using (Html.BeginForm()) { %>
    <div>
        <fieldset>
            <legend>Account Information</legend>
            <p>
                <label for="firstname">First Name:</label>
                <%= Html.TextBox("firstname", Model.FirstName) %>
                <%= Html.ValidationMessage("firstname") %>
            </p>
            <p>
                <label for="lastname">Last Name:</label>
                <%= Html.TextBox("lastname", Model.LastName) %>
                <%= Html.ValidationMessage("lastname") %>
            </p>
            <p>
                <label for="username">Username:</label>
                <%= Html.TextBox("username", Model.UserName) %>
                <%= Html.ValidationMessage("username") %>
            </p>
            <p>
                <label for="email">Email:</label>
                <%= Html.TextBox("email", Model.Email) %>
                <%= Html.ValidationMessage("email") %>
            </p>
            <p>
                <label for="zipcode">Zip Code:</label>
                <%= Html.TextBox("zip", Model.Zip) %>
                <%= Html.ValidationMessage("zip") %>
            </p>
            <p>
                <label for="password">Password:</label>
                <%= Html.Password("password", Model.Password) %>
                <%= Html.ValidationMessage("password") %>
            </p>
            <p>
                <label for="confirmPassword">Confirm password:</label>
                <%= Html.Password("confirmPassword",Model.PasswordConfirm) %>
                <%= Html.ValidationMessage("confirmPassword") %>
            </p>
            <p>
                <input type="submit" value="Register" />
            </p>
        </fieldset>
    </div>
<% } %>
+1  A: 

It's confirmPassword in the view, and PasswordConfirm in the model. It tries to serialize a field called confirmPassword to ConfirmPassword in the model, which isn't there

Sander Rijken
+2  A: 

You named the input confirmPassword but the property is named PasswordConfirm. These need to agree. Change your input to passwordConfirm and it should be bound properly.

tvanfosson
+2  A: 

The default model binding behavior is to hydrate your view model by matching HTML element names to view model property names. As a previous answer pointed out, the input element that you use to capture the confirm password is named "confirmPassword" and the view model property is "PasswordConfirm". If you update your code to make these names match, you should see the confirm password string in the model.

Andy Wilson
Thanks for the quick answers. Andy gets the check as he was first.
Jason Miesionczek