Row*_*haw 3 c# data-annotations
我装饰了一堂课:
[Required(ErrorMessage = "Price is required.")]
public decimal Price { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是当用代码验证它时:
for each (PropertyInfo prop in Me.GetType().GetProperties())
{
if (prop.GetIndexParameters().Length = 0)
{
for each (ValidationAttribute validatt in prop.GetCustomAttributes(GetType(ValidationAttribute), True))
{
if (!validatt.IsValid(prop.GetValue(Me, Nothing))
{
retval.Add(New PropertyValidationError(prop.Name, string.Format("There is a problem with the {0} property. It is flagged with the {1}", prop.Name, validatt.GetType.Name), validatt.ErrorMessage));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我发现 的值0被视为满足“要求”,这不是我想要的(实际上,我想允许除零以外的任何值) - 是我的验证代码做错了,还是有没有一种方法可以将装饰与 a 一起ValidationAttribute用于值类型的默认值?
| 归档时间: |
|
| 查看次数: |
1610 次 |
| 最近记录: |