1
[RequiredIf("Typeofproperty", "Typeofproperty != 5 && Typeofproperty != 2", ErrorMessage = "Do you need Two-wheeler parking ?")]        
 public int Twowheelerparking { get; set; }

Here Typeofproperty is of type int and control used for it is a radio button. 5 and 2 are the values of radio buttons for Plot and House. I want to validate Twowheelerparking if it is not a plot and house. The code above is not working for this purpose. Please tell me how to do it.

Smoking monkey
  • 323
  • 1
  • 6
  • 22
  • You need to post the code for the `RequiredIf` attribute (or is this from `Foolproof` or some other library? –  Sep 12 '14 at 06:38
  • Yes I'm using Foolproof. – Smoking monkey Sep 12 '14 at 06:40
  • @StephenMuecke - Please tell me what should I do now ? – Smoking monkey Sep 12 '14 at 07:02
  • Your not using the attribute correctly. Is should be something like `[RequiredIf("Typeofproperty", "2", ErrorMessage = "...")]`. And I don't think you can have multiple conditions. [This answer](http://stackoverflow.com/questions/17795681/how-to-make-a-property-required-based-on-multiple-condition) may point you in the right direction. –  Sep 12 '14 at 07:06
  • I have tried your way but it is showing the error message in any condition. – Smoking monkey Sep 12 '14 at 07:20
  • If there is any other way please let me know. Foolproof is not working here properly. – Smoking monkey Sep 12 '14 at 07:22

2 Answers2

0

Another simple way for obtaining desired result you can create your own validation attribute and in its isValid method definition you can use validationContext for validating data.

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.validationattribute_methods(v=vs.110).aspx

0

You may checkout the : following blog post for a sample implementation of a [RequiredIf] custom validation attribute. It compares against a single other property value but you could easily tweak the IsValid method to meet your requirements.