I have been working on this problem for 2 days now and it's an easy problem and I just can't see the error in the code, even after comparing it with other projects where this works.
Could you help out?
I'm working on the account section of my website using ASP.NET Membership and the account controller class that is generated with ASP.NET MVC.
However when registering and the user leaves some fields blank the entered text dosn't show up when the page is redisplayed.
Here is the register post action
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(FormCollection formValues)
{
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
if (ValidateRegistration(formValues))
{
// Attempt to register the user
MembershipCreateStatus createStatus = MembershipService.CreateUser(formValues["username"], formValues["password"], formValues["email"]);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuth.SignIn(formValues["username"], false /* createPersistentCookie */);
return RedirectToAction("CreateCustomer", "Account");
}
else
{
ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus));
}
}
// If we got this far, something failed, redisplay form
return View();
}
And the ValidateRegistration function
private bool ValidateRegistration(FormCollection formValues)
{
if (String.IsNullOrEmpty(formValues["userName"]))
{
ModelState.AddModelError("username", "You must specify a username.");
}
if (String.IsNullOrEmpty(formValues["email"]))
{
ModelState.AddModelError("email", "You must specify an email address.");
}
if (formValues["password"] == null || formValues["password"].Length < MembershipService.MinPasswordLength)
{
ModelState.AddModelError("password",
String.Format(CultureInfo.CurrentCulture,
"You must specify a password of {0} or more characters.",
MembershipService.MinPasswordLength));
}
if (!String.Equals(formValues["password"], formValues["confirmPassword"], StringComparison.Ordinal))
{
ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");
}
if (!String.Equals(formValues["email"], formValues["confirmEmail"], StringComparison.Ordinal))
{
ModelState.AddModelError("confirmEmail", "The email and confirmation email addresses do not match.");
}
return ModelState.IsValid;
}
And just in case here is the Register view
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
Register
Create a New Account
Use the form below to create a new account.
Passwords are required to be a minimum of <%=Html.Encode(ViewData["PasswordLength"])%> characters in length.
<%= Html.ValidationSummary() %>
<% using (Html.BeginForm()) { %> Username: <%= Html.TextBox("username")%>
<%= Html.ValidationMessage("username") %>
</td>
</tr>
<tr>
<td>Email:</td>
<td><%= Html.TextBox("email") %>
<%= Html.ValidationMessage("email") %>
</td>
</tr>
<tr>
<td>Confirm Email:</td>
<td>
<%= Html.TextBox("confirmEmail") %>
<%= Html.ValidationMessage("confirmEmail") %>
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<%= Html.Password("password") %>
<%= Html.ValidationMessage("password") %>
</td>
</tr>
<tr>
<td>Confirm password:</td>
<td>
<%= Html.Password("confirmPassword") %>
<%= Html.ValidationMessage("confirmPassword") %>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Register" />
</td>
</table>
</div>
<% } %>