0

This question mayt sound duplicate but I am not satisfied with any of the answers as some are suggesting MVC Foolproof validaiton for conditional validaiton and some tells it dont work well with entity framework

I am using MVC Foolproof RequiredIf validation in my project.It works well on clientside and is validation is working on server side as well.

 [RequiredIf("STCompulsory",Operator.EqualTo,true,ErrorMessage="Please enter Registration No")]
    public string STRegNo { get; set; }

But when i call db.Savechanges() to insert data an exception is coming

An unexpected exception was thrown during validation of 'STRegNo' when invoking 
Foolproof.RequiredIfAttribute.IsValid. See the inner exception for details.

InnerException

The method or operation is not implemented.
ksg
  • 3,927
  • 7
  • 51
  • 97
  • Is this applied to a view model or your actual data model? –  Oct 19 '15 at 09:44
  • Thanks for the reply..Its applied to actual data model.. – ksg Oct 19 '15 at 09:45
  • Not a good idea. And yes, there are some issues with using foolproof validations and EF so best to use a view model. Also it really only needs to be `[RequiredIf("STCompulsory", true, ErrorMessage = "..")]` –  Oct 19 '15 at 09:49
  • Thanks mate.You saved me again.If you post the answer i will accept it so that it helps others having similar issue.. – ksg Oct 19 '15 at 10:28
  • @StephenMuecke:If possible kinldy look into [this question](http://stackoverflow.com/questions/33346189/complex-custom-validation-in-foolproof-validation) – ksg Oct 27 '15 at 05:29
  • Were you wanting client side validation for that? –  Oct 27 '15 at 05:37
  • Yes..both client and server side validation.. – ksg Oct 27 '15 at 06:19

1 Answers1

1

You do not need the Operator.EqualTo parameter and it can be simply

[RequiredIf("STCompulsory", true, ErrorMessage="Please enter Registration No")
public string STRegNo { get; set; }

You are correct in that the foolproof [RequiredIf] attribute does have some problems with EF and it is discussed in detail in this work issue (along with some suggested changes).

The easiest solution is to use a view model rather than your data model in the view, and apply the attribute to the view model property.