Omu
Omu

Reputation: 71188

ModelState.IsValid == false, why?

Where can I find the list of errors of which make the ModelState invalid? I didn't see any errors property on the ModelState object.

Upvotes: 154

Views: 195561

Answers (11)

Pijush Mondal
Pijush Mondal

Reputation: 1

If ModelState.IsValid == false. Then check the .cs file validations that you have added. Remove the validations. It is a good practice to add customize validations according to the view file which you are using. This validations are restricting the modelState to render. Hope it helps .

Upvotes: -1

Mohammad Taheri
Mohammad Taheri

Reputation: 464

Visual Studio 2022 + dotnet 6 update:

I had the same problem for a long time and finally I found it. In my case, it was the Id field :)

Just place a breakpoint and check your ModelState in runtime and go to this section :

ModelState -> Root -> Children

and you will see all valid and invalid Keys

enter image description here

Upvotes: 0

cdsln
cdsln

Reputation: 888

I pasted some JSON into MS Teams Wiki page for future reference, when I copied it back out for use, it added extra invisible characters. I confirmed this by linting it at JSONLint

Removing the extra characters fixed this error for me.

Upvotes: -1

Tom McDonough
Tom McDonough

Reputation: 1262

If you remove the check for the ModelsState.IsValid and let it error, if you copy this line ((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors and paste it in the watch section in Visual Studio it will give you exactly what the error is. Saves a lot of time checking where the error is.

Upvotes: 6

Krishna
Krishna

Reputation: 8486

Paste the below code in the ActionResult of your controller and place the debugger at this point.

var errors = ModelState
    .Where(x => x.Value.Errors.Count > 0)
    .Select(x => new { x.Key, x.Value.Errors })
    .ToArray();

Upvotes: 90

AndyP9
AndyP9

Reputation: 626

As has just happened to me - this can also happen when you add a required property to your model without updating your form. In this case the ValidationSummary will not list the error message.

Upvotes: 3

queen3
queen3

Reputation: 15501

About "can it be that 0 errors and IsValid == false": here's MVC source code from https://github.com/Microsoft/referencesource/blob/master/System.Web/ModelBinding/ModelStateDictionary.cs#L37-L41

public bool IsValid {
    get {
        return Values.All(modelState => modelState.Errors.Count == 0);
    }
}

Now, it looks like it can't be. Well, that's for ASP.NET MVC v1.

Upvotes: 48

bastijn
bastijn

Reputation: 5953

As you are probably programming in Visual studio you'd better take advantage of the possibility of using breakpoints for such easy debugging steps (getting an idea what the problem is as in your case). Just place them just in front / at the place where you check ModelState.isValid and hover over the ModelState. Now you can easily browse through all the values inside and see what error causes the isvalid return false.

modelstate

Upvotes: 305

Jonas Stensved
Jonas Stensved

Reputation: 15276

Sometimes a binder throwns an exception with no error message. You can retrieve the exception with the following snippet to find out whats wrong:

(Often if the binder is trying to convert strings to complex types etc)

 if (!ModelState.IsValid)
            {
var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));

// Breakpoint, Log or examine the list with Exceptions.

  }

Upvotes: 17

Michael G
Michael G

Reputation: 6745

bool hasErrors =  ViewData.ModelState.Values.Any(x => x.Errors.Count > 1);

or iterate with

    foreach (ModelState state in ViewData.ModelState.Values.Where(x => x.Errors.Count > 0))
    {

    }

Upvotes: 26

tvanfosson
tvanfosson

Reputation: 532445

The ModelState property on the controller is actually a ModelStateDictionary object. You can iterate through the keys on the dictionary and use the IsValidField method to check if that particular field is valid.

Upvotes: 4

Related Questions