views:

126

answers:

2

Hi

I have a Registaration page with the following fields Email, Confirm Email, Password and Confrim Password.

On Register Button click and post the model to the server, the Model validates and if that Email is already Registered, it displays the Validation Error Message "User already Exists. Please Login or Register with a different email ID".

While we are displaying this validation error message, I am loosing the value of "Confirm Email" field. So that the user has to reenter again and I want to avoid this.

Here I don't have confirm_Email field in my Model.

Is there something special that has to be done to remain Confirm Email value on the Page even in case of Validation failure?

Appreciate your responses.

Here is my Code:

<% using (Html.BeginForm()) {%>
    <%= Html.ValidationSummary(false) %>

    <fieldset>
    <div class="cssform">

        <p>
            <%= Html.LabelFor(model => model.Email)%><em>*</em>
            <%= Html.TextBoxFor(model => model.Email, new { @class = "required email" })%>
            <%= Html.ValidationMessageFor(model => model.Email)%>
        </p>

        <p>
            <%= Html.Label("Confirm email")%><em>*</em>
            <%= Html.TextBox("confirm_email")%>
            <%= Html.ValidationMessage("confirm_email") %>
        </p>

        <p>
            <%= Html.Label("Password")%><em>*</em>
            <%= Html.Password("Password", null, new { @class = "required" })%>
            <%= Html.ValidationMessage("Password")%><br />
            (Note: Password should be minimum 6 characters)
        </p>

        <p>
            <%= Html.Label("Confirm Password")%><em>*</em>
            <%= Html.Password("confirm_password")%>
            <%= Html.ValidationMessage("confirm_password") %>
        </p><hr />

        <p>Note: Confirmation email will be sent to the email address listed above.</p>
    </fieldset>

<% } %>
+3  A: 

It's because it is not using your Model.

All your other fields are using TextBoxFor and that one is not so it isn't getting auto wired up to post back and forth pre-filling your model.

If you want it to have the value, your need to manually put it back in to the control or add it to your Model and it will automatically fill on your controller and when you pass the model back to your view it will build with the value that was pre-existing.

You can also add it's value to the ViewData the same name as the control's ID.

Either way you need to fetch the value in your controller and then pass it back to the view so the error message can be displayed. If you add it to your Model then it will be done automatically for you.

This is why it's nice to have a seperate view model that wraps all the data in your view and not just what you want to save to your DB. Your view shouldn't reflect only what your DB wants...

Kelsey
A: 

Since the property doesn't exist on your model, you can add a value to be persisted into the ModelState dictionary and it will automatically show up when you redisplay the form. Something like:

ModelState.Add("confirm_email", Request.Form["confirm_email"]);

Malevolence
I m trying to Add the Key to the ModelState as you recommended. But I am running into errors.
Rita