Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Asp.net core FluentValidation:如何显示客户端验证_Asp.net Core_Unobtrusive Validation_Fluentvalidation - Fatal编程技术网

Asp.net core FluentValidation:如何显示客户端验证

Asp.net core FluentValidation:如何显示客户端验证,asp.net-core,unobtrusive-validation,fluentvalidation,Asp.net Core,Unobtrusive Validation,Fluentvalidation,我正在使用FluentValidation public class AddressModel{ public string Street{get; set;} public string City{get; set;} public string State{get; set;} public string Zip{get; set;} public string Country{get; set;} } public class PersonModel{ publi

我正在使用FluentValidation

public class AddressModel{
  public string Street{get; set;}
  public string City{get; set;}
  public string State{get; set;}
  public string Zip{get; set;}
  public string Country{get; set;}
}

public class PersonModel{
  public string FirstName {get; set;}
  public string LastName {get; set;}
  public string SSN {get; set;}
  public AddressModel Address{get; set;}
  public AddressModel MailingAddress{get; set;}
  public int Type {get; set;}
}

 public class AddressValidator : AbstractValidator<AddressModel>
{
  public AddressValidator()
  {
    RuleFor(p=>p.Street).NotEmpty().WithMessage("Street is required");
    RuleFor(p=>p.City).NotEmpty().WithMessage("City is required");
    RuleFor(p=>p.Country).NotEmpty().WithMessage("Country is required");
    RuleFor(p=>p.State).NotEmpty().WithMessage("State is required"); 
    RuleFor(p=>p.Zip).NotEmpty().WithMessage("Zip code is required")
  }
}

public class PersonValidator : AbstractValidator<PersonModel>
{
  public PersonValidator()
  {
    RuleFor(p=>p.FirstName).NotEmpty().WithMessage("First name is required");
    RuleFor(p=>p.LastName).NotEmpty().WithMessage("Last name is required");
    RuleFor(p=>p.Address).SetValidator(new AddressValidator());
    RuleFor(p=>p.SSN).NotEmpty().WithMessage("SSN is required").When(p=>p.Type ==1); 
  }
}
公共类地址模型{
公共字符串Street{get;set;}
公共字符串City{get;set;}
公共字符串状态{get;set;}
公共字符串Zip{get;set;}
公共字符串国家{get;set;}
}
公共类个人模型{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串SSN{get;set;}
公共地址模型地址{get;set;}
公共地址模型MailingAddress{get;set;}
公共int类型{get;set;}
}
公共类AddressValidator:AbstractValidator
{
公共地址验证程序()
{
规则(p=>p.Street).NotEmpty().WithMessage(“Street是必需的”);
RuleFor(p=>p.City).NotEmpty().WithMessage(“需要城市”);
RuleFor(p=>p.Country).NotEmpty().WithMessage(“国家是必需的”);
RuleFor(p=>p.State).NotEmpty().WithMessage(“State是必需的”);
RuleFor(p=>p.Zip).NotEmpty().WithMessage(“需要邮政编码”)
}
}
公共类PersonValidator:AbstractValidator
{
公共PersonValidator()
{
RuleFor(p=>p.FirstName).NotEmpty().WithMessage(“需要名字”);
RuleFor(p=>p.LastName).NotEmpty().WithMessage(“需要姓氏”);
RuleFor(p=>p.Address).SetValidator(newAddressValidator());
RuleFor(p=>p.SSN).NotEmpty().WithMessage(“需要SSN”)。当(p=>p.Type==1);
}
}
//启动

services.AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
        .AddFluentValidation(fv =>
          {
             fv.RegisterValidatorsFromAssemblyContaining<Startup>();
          });
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddFluentValidation(fv=>
{
fv.RegisterValidatorsFromAssemblyContaining();
});
当我在UI上单击Save时,我将获得以下必填字段:FirstName、LastName和Address。在填写所有标记的必填字段并再次单击“保存”后,我将从服务器获取需要的MailingAddress和SSN(我选择了type=1)

我需要做什么才能不验证MailingAddress? 是否可以在客户端进行SSN验证


感谢您的帮助

SSN客户端:我想您需要编写一个自定义客户端适配器。开箱即用的客户端支持非常有限,并且可能由于您附加的When子句而无法启动

当我不得不建造自己的房子时,我曾作为向导


MainAddress服务器端:在调用服务器端验证之前,是否在您的模型中填充该地址?或者它是空的?如果没有定义明确的规则,我会期望验证器忽略它。有一个
Null()
规则可应用于属性以确保其为Null。

是否包括
\u validationscriptial.cshtml
?是。包括我在内。