ASP.NET MVC - ValidationSummary set from a different controller

Posted by Rap on Stack Overflow See other posts from Stack Overflow or by Rap
Published on 2010-12-23T20:40:41Z Indexed on 2010/12/23 21:53 UTC
Read the original article Hit count: 222

Filed under:
|
|

I have a HomeController with an Index action that shows the Index.aspx view. It has a username/password login section. When the user clicks the submit button, it POSTs to a Login action in the AccountController.

        <% Html.BeginForm("Login", "Account", FormMethod.Post); %>

In that action, it tests for Username/Password validity and if invalid, sends the user back to the Login page with a message that the credentials were bad.

    [HttpPost]
    public ActionResult Login(LoginViewModel Model, string ReturnUrl)
    {
        User user = MembershipService.ValidateUser(Model.UserName, Model.Password);
        if (user != null)
        {
            //Detail removed here
            FormsService.SignIn(user.ToString(), Model.RememberMe);
            return Redirect(ReturnUrl);
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
        // If we got this far, something failed, redisplay form
        return RedirectToAction("Index", "Home");  // <-- Here is the problem.  ModelState is lost.
    }

But here's the problem: the ValidationSummary is always blank because we're losing the Model when we RedirectToAction.

How do I send the user to the action on a different controller without a Redirect?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about validation