DataAnnotations MaxLength and StringLength for string[] or List<string>?

c#, data-annotations

Solution

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

Problem

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.

Original source