MVC5将两个可为空的日期与流利的验证进行比较 [英] MVC5 comparing two nullable dates with fluent validation

查看:48
本文介绍了MVC5将两个可为空的日期与流利的验证进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在流利的验证中编写规则以检查两个可为空的日期,因为开始日期必须早于结束日期.

How can I write a rule in fluent validation to check two nullable dates in that the start date needs to be earlier than the end date.

我正在考虑

RuleFor(c => c.StartDate)
            .NotEmpty()

如果开始日期不为空,结束日期不为空,则进行比较

if the start date is not empty and end date not empty then compare

推荐答案

类似这样的

RuleFor(ac => ac.StartDate)
     .NotEmpty().WithMessage("*Required")

 RuleFor(ac => ac.EndDate)
     .NotEmpty().WithMessage("*Required")
     .GreaterThan(r => r.StartDate);

注意-

数据类型必须相同以便进行比较.

The datatypes must be same for comparison.

,或者从此来源-

Or more convinient from this source-

 RuleFor(m => m.StartDate)
            .NotEmpty()
            .WithMessage("Start Date is Required");

        RuleFor(m => m.EndDate)
            .NotEmpty().WithMessage("End date is required")
            .GreaterThan(m => m.StartDate.Value)
                            .WithMessage("End date must after Start date")
            .When(m => m.StartDate.HasValue);

这篇关于MVC5将两个可为空的日期与流利的验证进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆