Rectangle 27 1

c ModelState.IsValid ignores validation of the non nullable property in MVC?

EDIT: Here is additional info from Microsoft's docs to answer the question that you posted in your comment:

Model validation occurs prior to each controller action being invoked, and it is the action methods responsibility to inspect ModelState.IsValid and react appropriately. In many cases, the appropriate reaction is to return some kind of error response, ideally detailing the reason why model validation failed.

So as mentioned in the documentation:

Thanks Univ, I am however in doubt, whether this approach describes the situation. The key here is, that the Id field does not get validated at all, as it is not part of the modelstate object. To reiterate, the Id property does not go through validation at all, it doesn't have either a false nor a true state, as it isn't one of modelstate key,value pairs. Even though, the parameter variable, upon construction, has the Id property set to 0 (because that is the default for Int), the 0 is set there by the constructor because it is not recieved from the view.

Thanks, thats a good explanation there in that doc, that the validation will only be applied to items submitted by the form. Since the view doesn't send that property, it does not go through validation at all.

The RequiredAttribute attribute specifies that when a field on a form is validated, the field must contain a value. A validation exception is raised if the property is null, contains an empty string (""), or contains only white-space characters.

This is the expected result here is why: