Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Tuesday, July 1, 2014 6:45 AM
In action method Send at the bottom of this post I have an example of ModelState.IsValid.
I understand the validation is done in this way.In the client is all validation done that is possible like
checking required information
checking correct format like date and Email
If something is wrong at this point the user is given immediate feedback about how to correct the problem and the information is not sent to the server.
The user can correct and resubmit the request with the corrections that have been made.
When the user submit and the client validation is successful
the [httpSend]Send method is called and the ModelState.IsValid will return true.
Now to my question when will ModelState.IsValid return false ?
I have tested this and if you for example add this
ModelState.AddModelError("", "One or more error were found");
before ModelState.IsValid then ModelState.IsValid will return false.
The reason for checking ModelState.IsValid is two as I believe.
- If a Business rule is not valid you must use code like this
ModelState.AddModelError("", "Some business rule is not valid");
which will cause ModelState.IsValid to return false and the user can change the entered data - If the user have disable javascript to be able to run on client side.
Give me a comment about this if I have understood this correct.
[HttpPost]
public ActionResult Send(MessageModel model)
{
if (ModelState.IsValid)
{
return RedirectToAction("Thank you");
}
return View(model);
}
//Tony
All replies (6)
Tuesday, July 1, 2014 8:49 AM ✅Answered
Now to my question when will ModelState.IsValid return false ?
If your data does not match the guidelines of your defined model, it will return false.
Example:
[HttpPost]
public ActionResult Edit(Product formData)
{
if (ModelState.IsValid)
{
// do stuff with the data
}
return View(model);
}
In this example, if any of the data you submitted ('formData') does not fit the type of properties or their annotations of the given model type, the ModelState will return false, and your data will be returned back to the view with errors to provide the user with details of what the form expects.
Tuesday, July 1, 2014 9:18 AM ✅Answered
The ModelState.IsValid property will return false if the server-side validation on your Model that was posted (via an HttpPost or a form submission) if any of the properties on your particular Model did not meet their required validation.
For example, if you had the following model :
public class TestModel
{
[Required]
public string ExampleProperty { get; set; }
[MaxLength(10)]
public string AnotherProperty { get; set; }
public TestModel()
{
}
}
This example model has two seperate validation annotations ([Required] and [MaxLength]) which are going to be used to help validation your particular model against the data that it contains. Errors will be added to the ModelState, if it does not meet all of these requirements.
For example, if you were to have the following Controller Action :
[HttpPost]
public ActionResult Index(TestModel model)
{
// Let's assume that your ExampleProperty on your Model is null and your AnotherProperty field
// exceeds the maximum number of characters allowed for it (10).
// Validate it
if(ModelState.IsValid)
{
// Great! It passed validation (this won't happen using the data mentioned above)
}
else
{
// Otherwise it failed validation (errors will be added to the model)
}
// Add your non-property errors
ModelState.AddModelError("","One or more errors occurred.");
return View();
}
this would spit out the following within your View if you were using a ValidationSummary element :
You seem to have the right idea behind handling it, although it's important to remember that client-side validation and server-side validation (which this is) are two completely seperate but important concepts.
Tuesday, July 1, 2014 10:22 AM ✅Answered
ModelState.IsValid just returns (ModelState.Errors.Count > 0). The MVC parameter binder will set errors, and of course as your example you can add your own errors.
note: server code has no knowledge if client validation was performed.
Tuesday, July 1, 2014 5:32 PM
So basically I still mean that the reason to have ModelState.IsValid on the server side is to be able to check in case javascript has been disable on the client side and
also to check business rules which might be a problem to check in the View.
//Tony
Wednesday, July 2, 2014 1:33 AM
Hi Tojo,
For these scenario, the value of ModelState.IsValid will be false too.
Disable client-side validate:
@{ Html.EnableClientValidation(false);Using jQuery AJAX to submit the data manually without validate the data.
To conclude, to check the value of ModelState.IsValid is used to verify current model data are meet the requirements.
In other words, we shouldn’t trust the data of the client.
For the business rules, I think we could custom validate.
Thanks
Best Regards
Wednesday, July 2, 2014 7:35 AM
So basically I still mean that the reason to have ModelState.IsValid on the server side is to be able to check in case javascript has been disable on the client side and
also to check business rules which might be a problem to check in the View.
Yes.
The ModelState.IsValid property will compare your existing model that is posted and determine if it is valid based on any of the previously mentioned attributes (like [Required]) that might be decorating your model. This is completely separate from any client-side Javascript validation that you might be performing, however it is always a good idea to implement both client-side and server-side validation to avoid any bad data coming through.
