RequiredIf Conditional Validation for two variables in MVC4

一个人想着一个人 提交于 2019-12-03 13:36:19

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

My Project has RequiredIf in it.

[Required]
public int SalesID { get; set; }

[RequiredIf("SalesID==1", ErrorMessage = "License is required.")]
public string License{ get; set; }

It shows error message 'License is required.' when License is left blank only if SalesID is 1. License cannot be blank if SalesID is 1.

For your code it should be something like

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

It means if Sunday and Saturday are true you can allow Holiday property to be an Empty String.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!