【问题标题】:MVC FoolProof Validation showing exception while calling SaveChange methodMVC FoolProof Validation 在调用 SaveChange 方法时显示异常
【发布时间】:2015-10-19 09:35:48
【问题描述】:

这个问题听起来可能重复,但我对任何答案都不满意,因为有些人建议 MVC Foolproof validaiton 进行条件验证,有些人说它不适用于实体框架

我在我的项目中使用MVC Foolproof RequiredIf validation。它在客户端运行良好,并且验证也在服务器端运行。

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

但是当我调用db.Savechanges() 插入数据时,异常即将到来

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

内部异常

The method or operation is not implemented.

【问题讨论】:

  • 这是应用于视图模型还是您的实际数据模型?
  • 感谢您的回复..它已应用于实际数据模型..
  • 不是一个好主意。是的,使用万无一失的验证和 EF 存在一些问题,因此最好使用视图模型。而且它真的只需要[RequiredIf("STCompulsory", true, ErrorMessage = "..")]
  • 谢谢队友。你又救了我。如果你发布答案,我会接受它,以便帮助其他有类似问题的人..
  • @StephenMuecke:如果可能,请查看this question

标签: entity-framework asp.net-mvc-4 foolproof-validation


【解决方案1】:

你不需要Operator.EqualTo参数,它可以很简单

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

您是正确的,万无一失的[RequiredIf] 属性确实与EF 存在一些问题,并且在this work issue 中进行了详细讨论(以及一些建议的更改)。

最简单的解决方案是在视图中使用视图模型而不是数据模型,并将属性应用于视图模型属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2020-04-04
    • 2018-02-12
    • 2013-11-04
    • 1970-01-01
    相关资源
    最近更新 更多