Asp.net mvc 2 ASP.NET MVC 2:数据批注验证应符合约定

Asp.net mvc 2 ASP.NET MVC 2:数据批注验证应符合约定,asp.net-mvc-2,data-annotations,conventions,Asp.net Mvc 2,Data Annotations,Conventions,我有一个与资源一起使用的必需属性: public class ArticleInput : InputBase { [Required(ErrorMessageResourceType = typeof(ArticleResources), ErrorMessageResourceName = "Body_Validation_Required")] public string Body { get; set; } } 我希望将资源指定为约定,如下所示: public clas

我有一个与资源一起使用的必需属性:

public class ArticleInput : InputBase
{
    [Required(ErrorMessageResourceType = typeof(ArticleResources), ErrorMessageResourceName = "Body_Validation_Required")]
    public string Body { get; set; }
}
我希望将资源指定为约定,如下所示:

public class ArticleInput : InputBase
{
    [Required2]
    public string Body { get; set; }
}
基本上,
Required2
实现基于此数据的值:

ErrorMessageResourceType = typeof(ClassNameWithoutInput + Resources); // ArticleResources
ErrorMessageResourceName = typeof(PropertyName + "_Validation_Required"); // Body_Validation_Required

有什么办法可以实现这样的目标吗?也许我需要实现一个新的
ValidationAttribute

如果不为属性提供自定义适配器,我认为这是不可能的,或者至少是不可能的。在属性的构造函数中没有任何方法可以访问应用该属性的方法/属性。否则,您将无法获取类型或属性名称信息

如果为属性创建了适配器,然后将其注册到DataAnnotationsModelValidatorProvider,那么在GetClientValidationRules中,可以访问ControllerContext和模型元数据。因此,您可能能够派生正确的资源类型和名称,然后查找正确的错误消息并将其添加到属性的客户端验证规则中

public class Required2AttributeAdapter
    : DataAnnotationsModelValidator<Required2Attribute>
{
    public Required2AttributeAdapter( ModelMetadata metadata,
                                      ControllerContext context,
                                      Required2Attribute attribute )
        : base( metadata, context, attribute )
    {
    }

    public override IEnumerable<ModelClientValidationRule>
        GetClientValidationRules()
    {
        // use this.ControllerContext and this.Metadata to find
        // the correct error message from the correct set of resources
        //
        return new[] {
            new ModelClientValidationRequiredRule( localizedErrorMessage )
        };
    }
}

我认为如果不为属性提供自定义适配器,这是不可能的,或者至少是不可能的。在属性的构造函数中没有任何方法可以访问应用该属性的方法/属性。否则,您将无法获取类型或属性名称信息

如果为属性创建了适配器,然后将其注册到DataAnnotationsModelValidatorProvider,那么在GetClientValidationRules中,可以访问ControllerContext和模型元数据。因此,您可能能够派生正确的资源类型和名称,然后查找正确的错误消息并将其添加到属性的客户端验证规则中

public class Required2AttributeAdapter
    : DataAnnotationsModelValidator<Required2Attribute>
{
    public Required2AttributeAdapter( ModelMetadata metadata,
                                      ControllerContext context,
                                      Required2Attribute attribute )
        : base( metadata, context, attribute )
    {
    }

    public override IEnumerable<ModelClientValidationRule>
        GetClientValidationRules()
    {
        // use this.ControllerContext and this.Metadata to find
        // the correct error message from the correct set of resources
        //
        return new[] {
            new ModelClientValidationRequiredRule( localizedErrorMessage )
        };
    }
}

我可以将一个适配器与任何验证属性一起使用吗?只需使用
DataAnnotationsModelValidator
?@stacker-现有属性已经注册了适配器。内部字典也由确切的类型设置密钥,因此我认为每个属性类型都需要一个适配器。我可以使用一个适配器和任何验证属性吗?只需使用
DataAnnotationsModelValidator
?@stacker-现有属性已经注册了适配器。内部字典也由确切的类型设置键,因此我认为每个属性类型需要一个适配器。