我有一个跟随的模型类
public bool Saturday{ get; set; }
public bool Sunday{ get; set; }
public string Holiday{ get; set; }
Run Code Online (Sandbox Code Playgroud)
我想使用星期六和星期日字段在Holiday字段中使用RequiredIf条件.我可以使用如下
[RequiredIf("Sunday,Saturday",false)]
public string Holiday{ get; set; }
Run Code Online (Sandbox Code Playgroud)
所以我不知道如何在我的模型类中使用RequiredIf条件,所以请有人帮助我
我们正在尝试使用Foolproof验证注释[RequiredIf]来检查是否需要电子邮件地址.我们还创建了一个枚举,以避免在ViewModel中使用查找表ID.代码如下所示:
public enum NotificationMethods {
Email = 1,
Fax = 2
}
Run Code Online (Sandbox Code Playgroud)
然后在ViewModel中:
[RequiredIf("NotificationMethodID", NotificationMethods.Email)]
public string email {get; set;}
Run Code Online (Sandbox Code Playgroud)
在此Senario中,当电子邮件未填充但选择作为通知类型时,我们不会收到错误.相反,这可以按预期工作:
[RequiredIf("NotificationMethodID", 1)]
public string email {get; set;}
Run Code Online (Sandbox Code Playgroud)
我发现的唯一的另一个参考是:https://foolproof.codeplex.com/workitem/17245
似乎Foolproof Validation不适用于WEB API:
// POST api/values
public void Post(MyModel model)
{
}
public class MyModel
{
public int Id { get; set; }
public bool Condition { get; set; }
[RequiredIfFalse("Condition")]
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
小提琴手:
User-Agent: Fiddler
Content-Type: application/json; charset=utf-8
Host: localhost:3858
Content-Length: 24
{Id: 1, Condition: true}
Run Code Online (Sandbox Code Playgroud)
响应:
{"Message":"An error has occurred.","ExceptionMessage":"The method or operation is not implemented.","ExceptionType"
Run Code Online (Sandbox Code Playgroud)
相同的代码适用于经典的MVC 4 temaplate.
我MVC Foolproof validation在我的申请中使用.方案是我有一个名为CustomerType的下拉列表,其中包含以下值
Id Name
1 Student
2 Non-Employed
3 Employed
4 SelfEmployed
Run Code Online (Sandbox Code Playgroud)
我的视图模型中还有一个属性.public string CompanyAddress{ get; set; }我的目标是使CompanyAddress required if下拉列表具有值3 or 4
我尝试了以下内容
[Required(ErrorMessage = "Please select status of the customer", AllowEmptyStrings = false)]
public int? customerTypeID { get; set; }
public SelectList customerTypeList { get; set; }
[RequiredIf("IsCompanyAddressRequired", true, ErrorMessage = "please enter company address")]
public string CompanyAddress { get; set; }
public bool IsCompanyAddressRequired
{
get
{
if (customerTypeID == 3 …Run Code Online (Sandbox Code Playgroud)