I am trying to figure out if there is a proper way to achieve this with DataAnnotations:
Have an array or List of strings where the maximum number of elements in the array or List is 2 items and where each string may only be, say 255 characters long. Will this work:
[MaxLength(2)]
[StringLength(255)]
public string[] StreetAddress { get; set; }
I would rather not have to make a new class just to hold a string Value
property to constrain each string to 255 characters.
You can create your own validation attribute by inheriting from Validation Attribute like described here: How to: Customize Data Field Validation in the Data Model Using Custom Attributes
来源:https://stackoverflow.com/questions/23746105/dataannotations-maxlength-and-stringlength-for-string-or-liststring