对 MVC 代码中的验证应用替代

本文关键字:应用 验证 MVC 代码 | 更新日期: 2023-09-27 18:31:57

我有一个验证类(如下所示)

private bool IsValidCustom(CIv2_AnnualLeave_AL civ2_annualLeave_al)
{
  bool result = true;
if (civ2_annualLeave_al.AL_EndDate < civ2_annualLeave_al.AL_StartDate)
{
  ViewBag.ErrorMessage = "End Date should be greater than or equal to Start Date";
  result = false;
}
if (civ2_annualLeave_al.AL_EndDate.Year != civ2_annualLeave_al.AL_StartDate.Year)
{
 ViewBag.ErrorMessage = "Start Date and End Date should be of same year, please book seperate booking for different year.";
result = false;
}
if ((civ2_annualLeave_al.AL_StartDate.Year.Equals(Vie wBag.varCurrentYear)) && (civ2_annualLeave_al.AL_DaysRequested > ViewBag.varCurrentYearRemaining))
{
   ViewBag.ErrorMessage = "Days Requested cannot be greater than " + ViewBag.varCurrentYearRemaining + " for " + ViewBag.varCurrentYear;
 result = false;
 }
if ((civ2_annualLeave_al.AL_StartDate.Year.Equals(Vie wBag.varNextYear)) && (civ2_annualLeave_al.AL_DaysRequested > ViewBag.varNextYearRemaining))
{
  ViewBag.ErrorMessage = "Days Requested cannot be greater than " + ViewBag.varNextYearRemaining + " for " + ViewBag.varNextYear;
 result = false;
}
return result;

}

我需要编写一些代码来允许勾选复选框以覆盖此验证的第 3 步和第 4 步,但我坚持如何批准这一点。复选框可能civ2_annualLeave_al。AL_IsUnpaid。

任何帮助将不胜感激。

对 MVC 代码中的验证应用替代

在规则 3 之前添加此项,如果勾选复选框,则返回 true 并跳过其他两个测试

if(civ2_annualLeave_al.AL_IsUnpaid == true){
    return true;
}