I'm having issues dealing with a http error response from webapi when there are ModelState errors, namely the fact that 'ModelState' isn't a collection but properties so I can't loop through them.
I'm struggling to find any information on how others are dealing with this, it must be a common use case?
Controller
if (!ModelState.IsValid)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
Resulting JSON
{
"readyState":4,
"responseText":"{\"Message\":\"The request is invalid.\",\"ModelState\":{\"model.Property1\":[\"'Property1' should not be empty.\"],\"model.Property2\":[\"'Property2' should not be empty.\"],\"model.Property3\":[\"'Property3' should not be empty.\"]}}",
"responseJSON":{
"Message":"The request is invalid.",
"ModelState":{
"model.Property1":[
"'Property1' should not be empty."
],
"model.Property2":[
"'Property2' should not be empty."
],
"model.Property3":[
"'Property3' should not be empty."
]
}
},
"status":400,
"statusText":"Bad Request"
}