问题描述
这个问题听起来可能并不重复,但是我对任何答案都不满意,因为有些人建议使用MVC Foolproof validaiton
作为条件验证,而有些人则认为它与实体框架不能很好地配合
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
我在项目中使用MVC Foolproof RequiredIf validation
.它在客户端上运行良好,并且验证也在服务器端进行.
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; }
但是当我致电db.Savechanges()
插入数据时,就会出现异常
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.
推荐答案
您不需要Operator.EqualTo
参数,它可以很简单
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; }
您是正确的,因为万无一失的[RequiredIf]
属性的确存在EF问题,并且此工作问题(以及一些建议的更改).
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.
这篇关于MVC FoolProof验证在调用SaveChange方法时显示异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!