Labels

slider

Recent

Navigation

MVC ModelState: ModelState Return always false using MVC

modelstate.isvalid is false, modelstate in mvc, modelstate.isvalid always false, modelstate.isvalid always true

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.
ModelState-IsValid

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 results

Failure (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.

Relevant Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: