【发布时间】:2016-08-01 22:56:36
【问题描述】:
我有一个带有指示业务类型的单选按钮的 MVC 表单。如果业务类型是 Limited 或 PLC,表格会显示额外的公司注册号和注册办事处地址字段。如果单选按钮的值大于 2,我想确保两个字段都完成。
@Html.LabelFor(x => x.CorporateIdentityId, "Sole Trader")
@Html.RadioButtonFor(x => x.CorporateIdentityId, "1", new { @checked = "checked" })
@Html.LabelFor(x => x.CorporateIdentityId, "Partnership")
@Html.RadioButtonFor(x => x.CorporateIdentityId, "2")
@Html.LabelFor(x => x.CorporateIdentityId, "Limited Company")
@Html.RadioButtonFor(x => x.CorporateIdentityId, "3")
@Html.LabelFor(x => x.CorporateIdentityId, "Plc")
@Html.RadioButtonFor(x => x.CorporateIdentityId, "4")
所以我安装了万无一失的验证。作为一个傻瓜,我认为这将是一个理想的解决方案。
在我的视图模型中,我有:
public int CorporateIdentityId { get; set; }
[Required]
[GreaterThan("CorporateIdentityId")]
[DisplayName("Reg Number")]
public string CompanyRegistration { get; set; }
我想在 GreaterThan 属性中指定一个值,即:
[GreaterThan("CorporateIdentityId","2")]
但它似乎不喜欢那样。当然,似乎没有关于如何做到这一点的文档或示例。所有示例似乎都只显示了隐含值 - 当前日期、真/假等。没有可比较的明确值。
我只是对万无一失的验证提出了太多要求吗?如果是这样,我可以使用其他替代方法吗?
【问题讨论】:
-
万无一失的 GreaterThanAttribute 意味着如果你照原样应用它,那么
CompanyRegistration的值必须大于CorporateIdentityId的值 - 所以如果你选择了"Limited Company",那么@987654326 的值@ 必须是4(或更多)。您将需要编写自己的验证属性和 jquery 验证器方法来实现您想要的。
标签: validation asp.net-mvc-5 foolproof-validation