Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Commented Issue: Web Api ModelState validation is ignoring the DisplayAttribute [744]

$
0
0
Given a model with these data annotations:

public class Example
{
[Required]
[Display(Name = "Activity response")]
public string ActivityResponse { get; set; }
}

I would expect the model state error message to be "The Activity response field is required." Instead it is "The ActivityResponse field is required."
Comments: You can look at the DataAnnotationModelValidator class and look at the Validate method. As you can see, we have already pass the display name to the context. [SecuritySafeCritical] public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container) { // Per the WCF RIA Services team, instance can never be null (if you have // no parent, you pass yourself for the "instance" parameter). ValidationContext context = new ValidationContext(container ?? metadata.Model, null, null); context.DisplayName = metadata.GetDisplayName(); ValidationResult result = Attribute.GetValidationResult(metadata.Model, context); if (result != ValidationResult.Success) { return new ModelValidationResult[] { new ModelValidationResult { Message = result.ErrorMessage } }; } return new ModelValidationResult[0]; } This exception is actually being thrown from the validationAttribute, through the reflector, the error message should be constructed using the display name from the validation context.

Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>