我有一个名为 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; }
这会在你的模型中加入逻辑(这通常是不行的),但它会起作用。你可以改变你的验证是这样的:
[RequiredIf("CompanyAddressRequired", true, ErrorMessage = "Please enter company address")]
然后有一个像这样的 getter 属性:
public bool CompanyAddressRequired
{
get
{
return customerTypeID == 3 || customerTypeID == 4;
}
}
对于“TypeError: Cannot read properties of undefined (reading 'value')”问题,在页面上为新的 CompanyAddressRequired 属性添加一个隐藏的输入元素。
<input type="hidden" id="CompanyAddressRequired"/>
我仍在努力让可切换字段在我的前端提供验证摘要,但这解决了 JS 错误。