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

问题描述

我有一个模型类正在关注

I have a model class that is following

 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中两个变量的RequiredIf条件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:58