I annotated my model as this:
public class Instance
{
[Required]
public string Name { get; set; }
public string Description { get; set; }
[Required]
public string DBServer { get; set; }
[Required]
public string Database { get; set; }
}
In the post method I get a null for the value if nothing was sent but Model.State is true. How can the state be true if nothing was sent? The next problem is that the CreateErrorResponse method throws an exception when I call it (probably because the value was null).
public HttpResponseMessage Post([FromBody]Instance value)
{
if (value != null && ModelState.IsValid)
{
...
}
else
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
Edit: As it seems I didn't explain it right. I try now with some screenshots.
Case 1
I post a correct value using Fiddle and everything works as expected. ModelState.IsValid is true.
Case 2 I post a value with a missing required field (DBServer) and then again everything works as expected. ModelState.IsValid is false.
Case 3 My question. I send a post request with NO information and ModelState.IsValid is true. This seems very strange and I would like to know the reason. Thank you all for your answers.