I see some code that for model validation uses:
[ValidateModel]
and other code such as a sample WebAPI using this for model validation:
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Can anyone explain what is the difference and why for example would the sample WebAPI apps from Microsoft not just use the [ValidateModel]?
Are you sure that you have
[ValidateModel]
attribute out of the box? Because according to this link it is custom attribute that helps make code a little bit cleaner, because you could omitModelState.IsValid
statement in the controller, because if action implemented its Model data is already valid. I browsed the internet and all[ValidateModel]
implementations have similarModelState.IsValid
code statement:I am not sure, but I think, because
[ValidateModel]
is custom attribute, which needs additional space for explanation and implementation, so most tutorials prefer out of the boxModelState.IsValid
.