自DataAnnotations验证从RegularEx pressionAttribute派生 [英] Custom DataAnnotations Validator Derived from RegularExpressionAttribute

查看:166
本文介绍了自DataAnnotations验证从RegularEx pressionAttribute派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

顾提供了可以如何创建一个覆盖RegularEx pressionAttribute自定义验证程序的示例

这样做的好处是,你不必创建一个自定义模式验证但我无法得到它的工作。

The advantage of this is that you don't have to create a custom Model Validator but I can't get it to work.

由于以下code:

public class NameAttribute : RegularExpressionAttribute {
    public NameAttribute()
        : base(@"^[\w\s\-\']+$") {
    }
}

本作品:

[RegularExpression(@"^[\w\s\-\']+$")]

但是,这并不:

[Name]

我有误解斯科特的例子的一个方面,或者处于MVC缺陷不支持派生类型现成的例子,所以实际上我将不得不创建一个相应的ModelValidator?

Have I misunderstood an aspect of Scott's example or is the example flawed in that MVC doesn't support derived types out of the box, so actually I will have to create a a corresponding ModelValidator?

推荐答案

破解了!下面添加到Global.asax.cs中的Application_Start()

Cracked it! Add the following to Global.asax.cs Application_Start()

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(NameAttribute), typeof(RegularExpressionAttributeAdapter));

这篇关于自DataAnnotations验证从RegularEx pressionAttribute派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆