如何使用 DataAnnotations 检查属性仅匹配字符串数组

Bac*_*Dao 5 c# asp.net arrays asp.net-mvc data-annotations

我有一个属性:

[MaxLength(3)]
public string State { get; set; }
Run Code Online (Sandbox Code Playgroud)

在指定的属性上State,我只希望它匹配给定的 5 个澳大利亚州: { "VIC", "NSW", "QLD", "SA", "TAS", "WA" }。我如何在这种情况下使用 DataAnnotations?

Kri*_*lla 4

您可以使用正则表达式属性

[RegularExpression("VIC|NSW|QLD|TAS|WA|SA")]
[MaxLength(3)]
public string State { get; set; }
Run Code Online (Sandbox Code Playgroud)

只允许维多利亚州、新南威尔士州、昆士兰州、塔斯马尼亚州、西澳州或南澳州