Introduction
I am today explaining one more very nasty error about ModelState Error. We daily work on MVC Apps using Model, sometime we get ModelState returning always false at that time, it is more required to diagnose our errors in shot period and take immediately action to remove our ModelState errors.Steps Provided to List ModelStae Error
1) ModelState will be always false, if the model required validation is failure.
2) You have to use Data Annotation using namespace System.ComponentModel.DataAnnotations to validate our Model.
using System.ComponentModel.DataAnnotations;
3) You can use also custom validations to validate your model.4) You also make assure that there should not null values for non null properties.
5) You can check your model error using ModelState Errors.
var errors = ModelState.Values.SelectMany(v => v.Errors);
ModelState Error is Mainly Two Types
Success (In case of ModelState Success)
Enumeration yielded no resultsFailure (In case of ModelState Failure)
Could not find a property named [Property/error].ModelStae Code Prototype
Here, I have provided ModelState code prototype which demonstrates, how can we check ModelState working or not.public ActionResult UpdatePassword(clsForgotPassword model)
{
if (ModelState.IsValid)
{
// do something here
}
}
Conclusion
ModelState is very common scene while working using MVC app. It takes more time to investigate ModelState error so it can result into wastage of our development time. I have provided very important precautions, exact scenario of issue in this article. Hopefully, it will help you all to my MVC app developers, while working with ModelState using MVC application.ModelState #ModelStateError Return always #ModelStateAlwaysfalse false using MVC #ModelStateMVC
http://t.co/udOJ92tqV1
— Anjan Kant (@AnjanKant) October 12, 2015
Post A Comment:
0 comments: