模型验证规则开始于并结束于

model validation rule starts with and ends with

我需要将验证器添加到 class 的 属性 以强制执行以下内容:

必须以 "TR" 或 "WE" 开头并以 3-4 位数字结尾。我有额外的验证规则,例如长度和必需的,但无法弄清楚如何执行上面概述的规则。我怀疑我必须以某种方式使用正则表达式,但我不确定语法。

public string Tree { get; set; }

尝试以下操作:

[RegularExpression("^(TR|WE)[a-z,A-Z]*[0-9]{3,4}$")]
public string Tree { get; set; }

您可以阅读 examples/more 信息的文档:https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.regularexpressionattribute(v=vs.110).aspx