MVC FoolProof Validation showing exception while calling SaveChange method

◇◆丶佛笑我妖孽 提交于 2019-12-12 03:24:12

问题


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.

回答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.



来源:https://stackoverflow.com/questions/33211126/mvc-foolproof-validation-showing-exception-while-calling-savechange-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!