Using Foolproof RequiredIf for Multiple Conditions

I have a drop-down list named CustomerType with the following values

Id     Name
1      Student
2      Non-Employed
3      Employed
4      SelfEmployed

and I have one more property in viewmodel public string CompanyAddress{ get; set; }

My goal is to make mandatory for CompanyAddress if dropdownlist matters 3 or 4

I tried the following but got an error Cannon have duplicate RequiredIf

    [RequiredIf("customerTypeID", 3, ErrorMessage = "Please enter company address")]
    [RequiredIf("customerTypeID", 4, ErrorMessage = "Please enter company address")]
    public string CompanyAddress { get; set; }
0
source share
1 answer

This will put the logic in your model (usually a no-no), but it will work. You can change your check like this:

[RequiredIf("CompanyAddressRequired", true, ErrorMessage = "Please enter company address")]

And then you have a getter property:

public bool CompanyAddressRequired
{
    get
    {
        return customerTypeID == 3 || customerTypeID == 4;
    }
}
+1
source

Source: https://habr.com/ru/post/1614564/


All Articles