无法设置在MVC2定制验证属性membernames [英] Unable to set membernames from custom validation attribute in MVC2
问题描述
我已经通过继承ValidationAttribute创建自定义验证属性。该属性在类级,因为它需要验证多个属性应用到我的视图模型。
我重写
保护覆盖的ValidationResult的IsValid(对象的值,ValidationContext validationContext)
和返回:
新的ValidationResult(总是失败,新的List<串GT; {出生日期});
在所有的情况下,出生日期是在我的视图模型的一个属性。
当我跑我的应用程序,我可以看到这个击中。 ModelState.IsValid正确设置为false,但是当我检查的ModelState内容,我看到房产DATEOFBIRTH不包含任何错误。相反,我有一个null值,并包含我在验证属性指定的字符串异常空字符串键。
这导致没有错误信息使用ValidationMessageFor时显示在我的UI。如果我使用的ValidationSummary,那么我可以看到错误。这是因为,它不与媒体资源相关联。
看起来好像是忽略了我在验证结果指定的membername的事实。
这是为什么,以及如何解决呢?
为例code的要求:
[AttributeUsage(AttributeTargets.Class,的AllowMultiple = FALSE,继承= TRUE)]
公共类ExampleValidationAttribute:ValidationAttribute
{
保护覆盖的ValidationResult的IsValid(对象的值,ValidationContext validationContext)
{
//注意,我会做多个属性的复杂的验证完成时,所以这就是为什么它是一个类级别属性
返回新的ValidationResult(总是失败,新的List<串GT; {出生日期});
}
} [ExampleValidation]
公共类ExampleViewModel
{
公共字符串DATEOFBIRTH {搞定;组; }
}
我不知道一个简单的方法解决这个问题。这就是为什么我讨厌数据注解的原因之一。做同样与 FluentValidation 将蛋糕和平:
公共类ExampleViewModelValidator:AbstractValidator< ExampleViewModel>
{
公共ExampleViewModelValidator()
{
RuleFor(X => x.EndDate)
.GreaterThan(X => x.StartDate)
.WithMessage(结束日期必须在开始日期之后);
}
}
FluentValidation有很大的支持和ASP.NET MVC 集成。
I have created a custom validation attribute by subclassing ValidationAttribute. The attribute is applied to my viewmodel at the class level as it needs to validate more than one property.
I am overriding
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
and returning:
new ValidationResult("Always Fail", new List<string> { "DateOfBirth" });
in all cases where DateOfBirth is one of the properties on my view model.
When I run my application, I can see this getting hit. ModelState.IsValid is set to false correctly but when I inspect the ModelState contents, I see that the Property DateOfBirth does NOT contain any errors. Instead I have an empty string Key with a value of null and an exception containing the string I specified in my validation attribute.
This results in no error message being displayed in my UI when using ValidationMessageFor. If I use ValidationSummary, then I can see the error. This is because it is not associated with a property.
It looks as though it is ignoring the fact that I have specified the membername in the validation result.
Why is this and how do I fix it?
EXAMPLE CODE AS REQUESTED:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ExampleValidationAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// note that I will be doing complex validation of multiple properties when complete so this is why it is a class level attribute
return new ValidationResult("Always Fail", new List<string> { "DateOfBirth" });
}
}
[ExampleValidation]
public class ExampleViewModel
{
public string DateOfBirth { get; set; }
}
I am not aware of an easy way fix this behavior. That's one of the reasons why I hate data annotations. Doing the same with FluentValidation would be a peace of cake:
public class ExampleViewModelValidator: AbstractValidator<ExampleViewModel>
{
public ExampleViewModelValidator()
{
RuleFor(x => x.EndDate)
.GreaterThan(x => x.StartDate)
.WithMessage("end date must be after start date");
}
}
FluentValidation has great support and integration with ASP.NET MVC.
这篇关于无法设置在MVC2定制验证属性membernames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!