ASP.NET MVC Page - Viewstate field for email confirmation gets erased on registration page if validation fails
I have a Registaration page with the following fields: Email, Confirm Email, Password, and Confrim Password.
In the Register window, click and submit the model to the server, the model validates, and if this email is already registered, it displays a validation error message "User already exists." Login or register with a different email ID. "
While we are displaying this validation error message, I am losing the Confirm Email Address field. To make the user come back again and I want to avoid this.
Here I have no confirm_Email field in my model.
Is there something special that needs to be done to stay on the Confirm Email icon on the page even if the verification fails?
Rate your answers.
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>
<% } %>
a source to share
This is because it doesn't use your model.
All of your other fields are using TextBoxFor
and it is not that it doesn't get automatically connected to the mailbox to pre-populate your model.
If you want it to have this value, you have to manually put it back in the control or add it to your model and it will auto populate on your controller and when you pass the model back to your view it will build with previously saved value.
You can also add it to ViewData
with the same name as the control ID.
Either way, you need to get the value in your controller and then pass it back to the view so that the error message can be displayed. If you add it to your model, it will be done automatically for you.
This is why it's nice to have a separate view model that wraps all the data in your view, not just what you want to store in your DB. Your opinion shouldn't just reflect what your DB wants ...
a source to share