在多重听取时使用Foolproof RequiredIf

时间:2015-11-25 05:24:06

标签: asp.net-mvc-4 unobtrusive-validation foolproof-validation

我有一个名为CustomerType的下拉列表,其中包含以下值

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

我的viewmodel public string CompanyAddress{ get; set; }

还有一个属性

我的目标是,如果下拉列表的值为3 or 4

,则需要使用CompanyAddress

我尝试过以下操作,但收到错误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; }

1 个答案:

答案 0 :(得分:3)

这将把逻辑放在你的模型中(这通常是禁止的),但它会起作用。您可以将验证更改为:

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

然后有一个像这样的吸气剂的财产:

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