1

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

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

我的财产中还有一处viewmodel public string CompanyAddress{ get; set; }

如果下拉列表具有值,我的目标是使 CompanyAddress 成为必需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; }
4

1 回答 1

3

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

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

然后有一个像这样的吸气剂的属性:

public bool CompanyAddressRequired
{
    get
    {
        return customerTypeID == 3 || customerTypeID == 4;
    }
}
于 2015-12-15T21:00:50.047 回答