views:

491

answers:

2

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()
            );
A: 

The only thing that is persisted here are the userName, password and rememberMe values. When the form is submitted, the userName, password and rememberMe are added to the ModelState automatically. The Html.TextBox() then grabs the value from the ModelState and inserts it into the HTML.

If you, for example, submit a bad userName, the controller adds a ModelState error. The form is rendered again, but you'll see that the userName is already filled with the old value. If you submit again, you'll obviously get the same error again.

chris166
I know but it seems to be keeping everything the same.
Daniel A. White
+2  A: 

Actually, it was my Castle Windsor configuration.

 container.Register(
        AllTypes
            .FromAssemblyNamed("Aplication")
            .BasedOn<IController>()
            .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))
            .WithService
            .FirstInterface()
        );

Its weird I can't do the based on and the where clause at the same time.

Daniel A. White