C# 使用 Validator 类验证 DataAnnotations

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2050161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:00:26  来源:igfitidea点击:

Validating DataAnnotations with Validator class

c#.netvalidation.net-4.0data-annotations

提问by Pablote

I'm trying to validate a class decorated with data annotation with the Validator class.

我正在尝试使用Validator class 验证用数据注释修饰的

It works fine when the attributes are applied to the same class. But when I try to use a metadata class it doesn't work. Is there anything I should do with the Validator so it uses the metadata class? Here's some code..

当属性应用于同一个类时,它工作正常。但是当我尝试使用元数据类时它不起作用。我应该对 Validator 做些什么以便它使用元数据类?这是一些代码..

this works:

这有效:

public class Persona
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "El nombre es obligatorio")]
    public string Nombre { get; set; }

    [Range(0, int.MaxValue, ErrorMessage="La edad no puede ser negativa")]
    public int Edad { get; set; }
}

this doesnt work:

这不起作用:

[MetadataType(typeof(Persona_Validation))]
public class Persona
{
    public string Nombre { get; set; }
    public int Edad { get; set; }
}

public class Persona_Validation
{
    [Required(AllowEmptyStrings = false, ErrorMessage = "El nombre es obligatorio")]
    public string Nombre { get; set; }

    [Range(0, int.MaxValue, ErrorMessage = "La edad no puede ser negativa")]
    public int Edad { get; set; }
}

this is how I validate the instances:

这就是我验证实例的方式:

ValidationContext context = new ValidationContext(p, null, null);
List<ValidationResult> results = new List<ValidationResult>();

bool valid = Validator.TryValidateObject(p, context, results, true);

thanks.

谢谢。

采纳答案by Jeremy Gruenwald

I found the answer here: http://forums.silverlight.net/forums/p/149264/377212.aspx

我在这里找到了答案:http: //forums.silverlight.net/forums/p/149264/377212.aspx

MVC recognizes the MetaDataType attribute, but other projects do not. Before validating, you need to manually register the metadata class:

MVC 识别 MetaDataType 属性,但其他项目不识别。在验证之前,您需要手动注册元数据类:

TypeDescriptor.AddProviderTransparent(
            new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Persona), typeof(Persona_Validation)), typeof(Persona));

ValidationContext context = new ValidationContext(p, null, null);
List<ValidationResult> results = new List<ValidationResult>();

bool valid = Validator.TryValidateObject(p, context, results, true);

回答by Dzejms

Try to move the metadata class into the same namespace as the Persona class if it isn't already. I was having similar problems and moving my metadata class into the same namespace as the L2S model class worked for me.

如果尚未将元数据类移动到与 Persona 类相同的命名空间中,请尝试将其移动。我遇到了类似的问题,并将我的元数据类移动到与 L2S 模型类为我工作的相同命名空间中。