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

Created Unassigned: DataAnnotationsModelValidator.Validate does not set MemberName [1670]

$
0
0
DataAnnotationsModelValidator.Validate should set the MemberName property of the ValidationContext. Only DisplayName is set.

Current Code:

```c#
public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
{
ValidationContext validationContext = new ValidationContext(container ?? metadata.Model, null, null)
{
DisplayName = metadata.GetDisplayName()
};
ValidationResult validationResult = this.Attribute.GetValidationResult(metadata.Model, validationContext);
if (validationResult == ValidationResult.Success)
{
return new ModelValidationResult[0];
}
ModelValidationResult[] modelValidationResultArray = new ModelValidationResult[1];
ModelValidationResult modelValidationResult = new ModelValidationResult()
{
Message = validationResult.ErrorMessage
};
modelValidationResultArray[0] = modelValidationResult;
return modelValidationResultArray;
}
```

There is:
DisplayName = metadata.GetDisplayName()

GetDisplayName is defined as:

```c#
public string GetDisplayName()
{
return this.PropertyName ?? this.ModelType.Name;
}
```

As you can see, there is access to the property name. In normal circumstances PropertyName will be filled in. Please change the code to have this:

ValidationContext validationContext = new ValidationContext(container ?? metadata.Model, null, null)
{
MemberName = metadata.PropertyName,
DisplayName = metadata.GetDisplayName()
};


Viewing all articles
Browse latest Browse all 7215

Trending Articles



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