简体   繁体   English

EF:IsValid()中的自定义数据验证

[英]EF: Custom Data Validation in IsValid()

I have a custom IsValid method in my model class. 我的模型类中有一个自定义IsValid方法。 Method looks like this: 方法如下:

public override bool IsValid(object value) {

 // How I can get field name here for some data manipulation. // some code for validation. 

}

How I can get field name which is calling this custom IsValid method from controller??? 我如何获取从控制器调用此自定义IsValid方法的字段名称??? I would really appreciate if you help me in this regard. 如果您在这方面帮助我,我将不胜感激。 Thanks in advance. 提前致谢。

I am taking a guess that this is what you mean. 我猜这就是你的意思。 Let's say that you know that the object you are validating is of type MyValidatableClass, having a property that should have the value "correct", you can do something like this: 假设您知道您要验证的对象的类型为MyValidatableClass,并具有应具有“正确”值的属性,则可以执行以下操作:

public override bool IsValid(object value)
{
  var objectToValidate = value as MyValidatableClass;

  // some code for validation.
  if (objectToValidate.SomeProperty != "correct")
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM