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>
}
Display Custom Error Messages Above, we added a custom error message using the ModelState. AddModelError() method. The ValidationSummary() method will automatically display all the error messages added into the ModelState .
Errors property and the ModelState. IsValid property. They're used for the second function of ModelState : to store the errors found in the submitted values.
Clear() is required to display back your model object. Posted on: April 19, 2012. If you are getting your Model from a form and you want to manipulate the data that came from the client form and write it back to a view, you need to call ModelState. Clear() to clean the ModelState values.
ModelState. IsValid property can be used to perform some logical operations based on the values submitted by the User. Note: If you want to learn about Client Side validations in ASP.Net MVC Razor, please refer ASP.Net MVC: Client Side validations using Data Annotation attributes and jQuery.
@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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With