相关疑难解决方法(0)

如何为MVC创建自定义验证属性

我想为MVC2创建一个自定义验证属性,用于不从RegularExpressionAttribute继承但可以在客户端验证中使用的电子邮件地址.谁能指出我正确的方向?

我尝试了一些简单的事情:

[AttributeUsage( AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false )]
public class EmailAddressAttribute : RegularExpressionAttribute
{
    public EmailAddressAttribute( )
        : base( Validation.EmailAddressRegex ) { }
}
Run Code Online (Sandbox Code Playgroud)

但它似乎对客户不起作用.但是,如果我使用RegularExpression(Validation.EmailAddressRegex)]它似乎工作正常.

asp.net-mvc asp.net-mvc-validation

12
推荐指数
1
解决办法
3万
查看次数

如何将RegularExpression DataAnnotation与Resource文件一起使用

我目前正在使用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表达式或数组创建表达式.

有没有办法使用资源文件中的值作为正则表达式,还是需要在每个具有邮政编码的模型中输入实际的正则表达式字符串?

谢谢

asp.net-mvc resources data-annotations

2
推荐指数
1
解决办法
2676
查看次数