Alex Rivera | Logout

Only show a div if the modelstate has errors

Asked 2012-02-21T10:17:36.203
10

I have a div which contains a generic styling and error message if a user enters the wrong username/password combo.

I only want to display this div if the user has entered the wrong information. Is there something on my View I could do, something like:

@if(ModelState.HasErrors)
{
      <div>This login failed</div>
}
Edit
Report

1 Answer

17
@if (!ViewData.ModelState.IsValid)
{
      <div>This login failed</div>
}

Side note, can't you use the built-in Validation helpers?

Controller:

if (yourError)
{
   ModelState.AddModelError("Error", "This login failed");
   return View();
}

View:

@Html.ValidationMessage("Error")
answered 2012-02-21T10:21:27.537

Your Answer