Using Foolproof RequiredIf on multiplecondition

若如初见. 提交于 2019-12-11 10:26:45

问题


I have a dropdownlist named CustomerType with the the following values

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

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

My goal is to make CompanyAddress required if dropdownlist has values 3 or 4

I have tried the following but gets 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; }

回答1:


This will put logic in your model (which is usually a no-no), but it will work. You could change your validation to be like this:

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

And then have a property with a getter like this:

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


来源:https://stackoverflow.com/questions/33909005/using-foolproof-requiredif-on-multiplecondition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!