如果在MVC4中对两个变量进行条件验证,则为必需 [英] RequiredIf Conditional Validation for two variables in MVC4

查看:76
本文介绍了如果在MVC4中对两个变量进行条件验证,则为必需的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个遵循的模型类

 public bool Saturday{ get; set; }

 public bool Sunday{ get; set; }

 public string Holiday{ get; set; }

我要在星期六和星期日字段的假日"字段中使用RequiredIf条件.我可以像

In which I want to use the RequiredIf condition for the Holiday field using the both Saturday and Sunday fields. Can I use like following

   [RequiredIf("Sunday,Saturday",false)]
   public string Holiday{ get; set; }

所以我不知道如何在模型类中使用RequiredIf条件,所以请有人帮助我

So I don't know how to use the RequiredIf condition in my model class, So please someone help me

推荐答案

也许可以在您的模型中尝试一下:

Maybe try this in your model:

[Required]
public bool Saturday{ get; set; }

[Required]
public bool Sunday{ get; set; }

[NotMapped]
public bool SatSun
{
    get
    {
        return (!this.Saturday && !this.Sunday);
    }
}

[RequiredIf("SatSun",true)]
public string Holiday{ get; set; }

这篇关于如果在MVC4中对两个变量进行条件验证,则为必需的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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