Unable to set membernames from custom validation attribute in MVC2

后端 未结 4 2222
甜味超标
甜味超标 2020-12-11 17:04

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 tha

4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-11 17:21

    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
    {
        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.

提交回复
热议问题