views:

363

answers:

3

I have a problem with validation messages not showing after a redirect, even when Im using MVCConrib's ModelStateToTempData. Am I overlooking something fundamental?

[ModelStateToTempData]
public class AccountController : BaseController
{
    public ActionResult LogOn(string email, string password, string returnUrl)
    {
        if (!ValidateLogOn(email, password))
        {
            return RedirectToAction("Index", "AnotherController");
        }

        //other stuff
    }

private bool ValidateLogOn(string email, string password)
{
    if (!_userTask.ValidateUser(email, password))
    {
        ModelState.AddModelError("message", "The email or password provided is incorrect.");
    }

    return ModelState.IsValid;
}
}

View:

   <li>
        <label for="email">E-mail</label>
        <%= Html.TextBox("email")%>
        <%= Html.ValidationMessage("message") %>
    </li>
A: 

Are you definitely displaying a validation message in the View?

IainMH
yeah :( - iv updated the question to reflect this
Dan
A: 

Are you decorating both controllers with [ModelStateToTempData]?

James

James S
Do I need to? I'll try it.
Dan
That did the trick - obvious now you say it.
Dan
A: 

You shouldn't redirect to any other controller if you have any error in the ModelState. There is no goot to navigate to other controller if there is error in ModelState - it's better to redirect on success, but not on fail.

Just check if ModelState contatins any errors and return the View you've recived request from.

 if (!ValidateLogOn(email, password))
 {
     return View("Index");
 }
Mike