Fer*_*min 2 asp.net-mvc resources data-annotations
我目前正在使用MVC 1.0和.NET 3.5.我正在使用DataAnnotations来验证我的模型.我正在尝试添加使用RegularExpression来验证邮政编码.当我尝试以下操作时,我已将我的正则表达式存储在资源文件中,因为许多模型将使用它
[RegularExpression(Resources.RegexPostcode, ErrorMessage="Postcode format invalid")]
public string Postcode { get; set; }
Run Code Online (Sandbox Code Playgroud)
我构建时遇到以下错误:
属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式.
有没有办法使用资源文件中的值作为正则表达式,还是需要在每个具有邮政编码的模型中输入实际的正则表达式字符串?
谢谢
我建议你自己制作ValidationAttribute.这将使正则表达式保持在一个位置以及错误消息.
class PostcodeAttribute : RegularExpressionAttribute
{
public PostcodeAttribute() : base("your regex")
{
this.ErrorMessage = "Postcode format invalid";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2676 次 |
| 最近记录: |