RequiredIf Conditional Validation for two variables in MVC4

后端 未结 3 1468
眼角桃花
眼角桃花 2021-02-08 04:29

I have a model class that is following

 public bool Saturday{ get; set; }

 public bool Sunday{ get; set; }

 public string Holiday{ get; set; }
3条回答
  •  梦如初夏
    2021-02-08 04:56

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

提交回复
热议问题