We have a couple of models that override the name through JsonProperty, but this causes a problem when we get validation errors through ModelState. For instance:
class MyModel
{
[JsonProperty("id")]
[Required]
public string MyModelId {get;set;}
}
class MyModelController
{
public IHttpActionResult Post([FromBody] MyModel model)
{
if (!ModelState.IsValid)
{
return HttpBadRequest(ModelState);
}
}
}
The above Mail will return an error The MyModelId field is required.that is inaccurate. We would like to say that The id field is required.. We tried to use [DataMember(Name="id")], but we get the same result.
Question 1: Is it possible to get ModelState errors to show the name of the JSON property, and not the name of the C # property, other than providing our own error messages for each attribute [Required]?
- Update -
" " . , , , , ...
https://gist.github.com/Blackbaud-JasonTremper/b64dc6ddb460afa1698daa6d075857e4
2. , ModelState.Key <parameterName>.<reflectedProperty> , ?
3: , JSON, [DataMember] [JsonProperty]?