RequiredIf Conditional Validation for two variables in MVC4

Md Aslam picture Md Aslam · Feb 5, 2015 · Viewed 22k times · Source

I have a model class that is following

 public bool Saturday{ get; set; }

 public bool Sunday{ get; set; }

 public string Holiday{ get; set; }

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; }

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

Answer

Chico Ribeiro picture Chico Ribeiro · Feb 5, 2015

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; }