MVC Foolproof Validation If Greater Than Value

1.5k Views Asked by At

I have an MVC form with radio buttons indicating a business type. If the business type is either a Limited or PLC, the form shows additional company registration number and registered office address fields. I want to ensure both fields are completed if the radio button value is greater than 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")

So I installed Foolproof Validation. being a fool I thought this would be an ideal solution.

In my view model I have:

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

I'd like to specify a value in the GreaterThan attribute ie:

[GreaterThan("CorporateIdentityId","2")]

But it don't seem to like that. And of course there seems to be no documentation or examples on how to do this. All the examples seem to just show implied values - current date, true/false etc. No explicit values to compare against.

Am I simply asking too much of Foolproof Validation? If so, is there an alternative I could use?

1

There are 1 best solutions below

0
On

Add Constructor in your 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