Is there a way that I can add non-static data to dataannotation attributes (either the standard attributes or a custom attribute that inherits from either a standard dataannotation (display, range, etc.) or the attribute base class)? I’m hoping to do something like this:
public class ReportingDateTime
{
[Display(Name=this.FieldName)]
[Reporting.Core.CustomDisplay(this.FieldName)]
public DateTime Field { get; set; }
private string FieldName;
public ReportingDateTime(string fieldName)
{
this.FieldName = fieldName;
}
}
Alternatively, is there a way to change the metadata for a property in the class’ constructor like so:
public class ReportingDateTime
{
public DateTime Field { get; set; }
private string FieldName;
public ReportingDateTime(string fieldName)
{
Field.metadata.DisplayName = "Test Date";
}
}
From what I’ve seen there has been some success passing the type of an object (the custom attribute expected a new instance of a custom object) but I’m primarily looking at simple data types (string, int, double) and perhaps generic collections (list, dictionary, etc.)
As far as I can tell there is no way to currently do this. What ended up working for me was to have custom editor/display templates for that class so I could use my own fields for the display property, like so:
For validation I’d recommend using FluentValidation’s conditional validation (http://fluentvalidation.codeplex.com/wikipage?title=Customising&referringTitle=Documentation&ANCHOR#WhenUnless) and use the properties in your class as the conditional statements.