Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如果字段为空,如何忽略验证注释?_.net_Entity Framework_Asp.net Mvc 5_Code First - Fatal编程技术网

.net 如果字段为空,如何忽略验证注释?

.net 如果字段为空,如何忽略验证注释?,.net,entity-framework,asp.net-mvc-5,code-first,.net,Entity Framework,Asp.net Mvc 5,Code First,我有以下课程: Class Parent { [Required] String Name ; Child child ; } Class Child { [Required] Child FirstName ; [Required] Child LastName ; } 我有一个显示父实体字段(包括子实体字段)的表单。在我的配置中,子项的FistName和LastName是必需的,如果留空将导致验证失败 我需要的是,如果孩子的名字和姓氏都提交为空

我有以下课程:

Class Parent
{
   [Required]
   String Name ;

   Child child ;
}

Class Child
{
   [Required]
   Child FirstName ;
   [Required]
   Child LastName ;
}
我有一个显示父实体字段(包括子实体字段)的表单。在我的配置中,子项的FistName和LastName是必需的,如果留空将导致验证失败

我需要的是,如果孩子的名字和姓氏都提交为空,则验证通过。如果提交了FirstName或LastName,则需要另一个,验证应失败


如何实现这一点?

只需将注释更改为:

[必需(AllowEmptyString=true)]

这将允许空字符串通过验证。然后,如果需要,可以在服务器上执行其他验证

此链接应有助于:


只需将注释更改为:

[必需(AllowEmptyString=true)]

这将允许空字符串通过验证。然后,如果需要,可以在服务器上执行其他验证

此链接应有助于:


我将在模型上实现您自己的验证方法。您的模型最终会看起来像这样:

public class Child : IValidatableObject {
   public string FirstName {get; set;}
   public string LastName {get; set;}

   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
       if ((string.IsNullOrEmpty(this.FirstName) && !string.IsNullOrEmpty(this.LastName)) || (!string.IsNullOrEmpty(this.FirstName) && string.IsNullOrEmpty(this.LastName))) {
           yield return new ValidationResult("You must supply both first name and last name or neither of them");
       }
   }
}
公共类子对象:IValidatableObject{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共IEnumerable验证(ValidationContext ValidationContext){
if((string.IsNullOrEmpty(this.FirstName)和&!string.IsNullOrEmpty(this.LastName))| |(!string.IsNullOrEmpty(this.FirstName)和&string.IsNullOrEmpty(this.LastName))){
返回新的ValidationResult(“您必须同时提供名字和姓氏,或者两者都不提供”);
}
}
}

我将在模型上实现您自己的验证方法。您的模型最终会看起来像这样:

public class Child : IValidatableObject {
   public string FirstName {get; set;}
   public string LastName {get; set;}

   public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
       if ((string.IsNullOrEmpty(this.FirstName) && !string.IsNullOrEmpty(this.LastName)) || (!string.IsNullOrEmpty(this.FirstName) && string.IsNullOrEmpty(this.LastName))) {
           yield return new ValidationResult("You must supply both first name and last name or neither of them");
       }
   }
}
公共类子对象:IValidatableObject{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共IEnumerable验证(ValidationContext ValidationContext){
if((string.IsNullOrEmpty(this.FirstName)和&!string.IsNullOrEmpty(this.LastName))| |(!string.IsNullOrEmpty(this.FirstName)和&string.IsNullOrEmpty(this.LastName))){
返回新的ValidationResult(“您必须同时提供名字和姓氏,或者两者都不提供”);
}
}
}

我想@Daniel是在问如何验证只提供了一个值(名字或姓氏)的场景。我想@Daniel是在问如何验证只提供了一个值(名字或姓氏)的场景。确切地说,我要建议的正是我要建议的