So I have an API that has a controller post
action like this:
public HttpResponseMessage Post(Model m){
if(!ModelState.IsValid){
return ApiRequest.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
And here is my model:
[MetadataType(typeof(Metadata))]
public partial class MyModel {
private sealed class Metadata {
[Required(ErrorMessage="Sample error msg.")]
public string Field1 {get; set;}
// ...other fields and foreign key fields here
}
}
As I stated on my title, I am using database first which is why I created a partial
class since my models are auto-generated.
The problem is when a validation error occurs the result JSON of error messages looks like this:
{
e.Field1: "Error msg here...",
e.Field2: "Error msg here...",
e.DateField: "An error occurred" //This happens for any date type field even though i've specified an error message
e: ["An error occurred", "An error occurred"] //This happens for the foreign key fields i.e. AnotherModelID, I got 2 which is why I think it has two error messages also
}
First what I want here is to remove the e
prefix on the keys
of the JSON result. Then why is the error An error occurred
being returned even though I've put an error message explicitly? And also why does the foreign key fields doesn't display just the prefix e
?
Thanks
UPDATE
Upon more research I think in the case where the error key
is only the letter e
instead of the e.FieldName
happens when the data type of the field in question is int
. I've tested a non-foreign key int field and it also adds to the error under the "e only" key.