Reputation:
I have a model with properties and validators (annotations) above most of them validating this or that.
Does a validator use the Model Property or does it use the raw value sent by the POST? In other words, are Properties set first, then validation occurs, or does validation occur first, then the Properties are set (if validation returns no error)?
Upvotes: 0
Views: 361
Reputation: 1082
You can check to see if your model was valid or not in the controller like so:
public class ProductsController : ApiController
{
public HttpResponseMessage Post(Product product)
{
if (ModelState.IsValid)
{
// Do something with the product (not shown).
return new HttpResponseMessage(HttpStatusCode.OK);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
}
More info: Model Validation in ASP.NET
Upvotes: 1