My controller actions:
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Login()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Login(string UserName, string Password, bool RememberMe)
{
var userNameValidationResults = _validatorProvider.Validate<IMailAddressValidator>(UserName);
foreach (var result in userNameValidationResults.Where(r => !r.Passed)) {
ModelState.AddModelError("UserName", result.ErrorMessage);
}
var passwordValidationResults = _validatorProvider.Validate<IStringLengthValidator>(Password);
foreach (var result in passwordValidationResults.Where(r => !r.Passed)) {
ModelState.AddModelError("Password", "Please enter a password.");
}
if (!ModelState.IsValid)
return View();
bool validUser = _userMembershipService.ValidateUser(UserName, Password);
if (!validUser) {
ModelState.AddModelError("_FORM", "Invalid e-mail address and/or password.");
return View();
}
_userAuthenicationService.Login(UserName, RememberMe);
return RedirectToAction("Index", "Home");
}
My view:
<h2>Login</h2>
<%= Html.ValidationSummary() %>
<% using (Html.BeginForm()) { %>
<%= Html.TextBox("UserName") %>
<%= Html.Password("Password") %>
<%= Html.CheckBox("RememberMe") %>
<input type="submit" value="Login" />
<% } %>
If I submit the form again after a validation error, I get another error (the same exact error) in the ValidationSummary
. Why is that happening? Thanks.
Edit What I mean is that I have n
-number of errors where n
is the number of submissions.
Edit 2 What I now am having a problem it looks like it is remaining persistant - even the form field values won't change. I updated my code above. Also, I am using Castle Windsor for an IoC container, but I don't think that would be the source of my problem. But for the case it could be, here is the source I am using to register my controllers.
container.Register(
AllTypes
.FromAssemblyNamed("Aplication")
.BasedOn<IController>()
.Where(t => t.Name.EndsWith("Controller"))
.Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
.WithService
.FirstInterface()
);