MVC万无一失的验证如果大于价值

时间:2015-08-10 14:14:39

标签: validation asp.net-mvc-5 foolproof-validation

我有一个带有指示商业类型的单选按钮的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")

所以我安装了Foolproof验证。我认为这将是一个理想的解决方案。

在我的视图模型中,我有:

public int CorporateIdentityId { get; set; }
[Required]
[GreaterThan("CorporateIdentityId")]
[DisplayName("Reg Number")]
public string CompanyRegistration { get; set; }

我想在GreaterThan属性中指定一个值,即:

[GREATERTHAN(" CorporateIdentityId"" 2&#34)]

但它似乎并不喜欢这样。当然,似乎没有关于如何执行此操作的文档或示例。所有示例似乎只显示隐含值 - 当前日期,真/假等。没有要比较的显式值。

我只是要求过多的万无一失的验证?如果是这样,我可以使用替代方案吗?

1 个答案:

答案 0 :(得分:0)

在GreaterThanAttribute中添加构造函数

 public class GreaterThanAttribute : Attribute
{
    private string _propertyName;
    private string _propertyvalue;

    public GreaterThanAttribute(string propertyName, string propertyvalue)
    {
        _propertyName = propertyName;
        _propertyvalue = propertyvalue;
        throw new NotImplementedException();
    }

   //your IsValid Logic 
相关问题