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

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-01 12:01:58

问题


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.


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!