6

I would like to know if there exists RequiredIf annotation in MVC 5 because I'm in trouble trying to use it because it seems that it doesn't exists in MVC 5. Is there any solution, or I'm missing some library? (I know it exists in MVC3).

Also is there any alternative for conditional validation in MVC 5?

Thanks in advance.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Shmwel
  • 1,697
  • 5
  • 26
  • 43
  • have you tried implement your attribute? http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx or http://stackoverflow.com/questions/7390902/requiredif-conditional-validation-attribute – giammin Oct 06 '14 at 13:58
  • 2
    Whoever downvoted explain please.. – Shmwel Oct 06 '14 at 13:58

1 Answers1

19

Required if is not a built-in attribute. You need to download and import third party library:

MVC Foolproof Validation

meda
  • 45,103
  • 14
  • 92
  • 122
  • I did as you mentioned above but still not working .Using it in the following way [RequiredIf("GSMEthernetMAC", "", ErrorMessageResourceType = "Please enter WiFiMac")] public string WifiMAC { get; set; } If GSMEthernetMAC is empty then only it will throw a message. – Viku Jun 01 '15 at 14:59
  • @vivek did you import the dll, which error do you see – meda Jun 01 '15 at 15:09
  • dll is imported.I forgot to add the js files .. After adding 3 js files (1 - mvcfoolproof.unobtrusive.min 2 - MvcFoolproofJQueryValidation.min. 3 - MvcFoolproofValidation.min)i am getting the below error Uncaught TypeError: Cannot read property 'addMethod' of undefined(anonymous function) @ mvcfoolproof.unobtrusive.min.js: – Viku Jun 01 '15 at 15:58
  • 1
    You should not have to import anything else but the dll, If you have trouble use the nugget package manager , see here https://www.nuget.org/packages/foolproof `Install-Package foolproof` – meda Jun 01 '15 at 16:28
  • I can able to make it work . Actually i forgot to add the dependent js files for foolproof plugins . I am facing one more issue that is when i am navigating to that page from another page its not working but when i hit ctrl+f5 for the same page , it works like a charm. Can you please help me figure it out ? – Viku Jun 02 '15 at 09:32