我有一个名为CustomerType的下拉列表,其中包含以下值
Id Name
1 Student
2 Non-Employed
3 Employed
4 SelfEmployed
我的viewmodel
public string CompanyAddress{ get; set; }
我的目标是,如果下拉列表的值为3 or 4
我尝试过以下操作,但收到错误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 :(得分:3)
这将把逻辑放在你的模型中(这通常是禁止的),但它会起作用。您可以将验证更改为:
[RequiredIf("CompanyAddressRequired", true, ErrorMessage = "Please enter company address")]
然后有一个像这样的吸气剂的财产:
public bool CompanyAddressRequired
{
get
{
return customerTypeID == 3 || customerTypeID == 4;
}
}