Domanda

My requirement is to validate a field depending on Ajax return success or failure message. I have hidden property IsValidEmployeenumber is set 'true' or 'false' from Ajax call.

    [XXX('IsValidEmployeeNumber', 'true')]  //Please suggest
    public string EmployeeNumber { get; set; }

    public string IsValidEmployeeNumber { get; set; }

May I know is there a native MVC data annotation available that will take dependent property name and expected value.

Thanks,

È stato utile?

Soluzione

No. There is no such native annotation. You can make your own, which will take as a paramter the name of the property on which the validation depends, and then when applying, pass the appropriate property name.

The way to do this is to create a custom annotation class deriving from ValidationAttribute, and making a constructor which takes as a parameter the name of the dependent property, and then override the IsValid method, in which you get the value of the dependent property using reflection, and write your custom validation logic.

Now, this will work for server side validation. But you need to do a lot more if you need client side validation as well, which is beyond the scope of this question.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top