Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/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
C# FluentValidation-验证包含对象列表的视图模型_C#_Asp.net Mvc 3_Fluentvalidation - Fatal编程技术网

C# FluentValidation-验证包含对象列表的视图模型

C# FluentValidation-验证包含对象列表的视图模型,c#,asp.net-mvc-3,fluentvalidation,C#,Asp.net Mvc 3,Fluentvalidation,我正在一个包含复杂视图模型的项目上尝试FluentValidation,我阅读了,但我不知道如何设置规则来验证视图模型中声明的对象列表。在下面的示例中,视图模型中的列表包含一个或多个吉他对象。谢谢 查看模型 [FluentValidation.Attributes.Validator(typeof(CustomerViewModelValidator))] public class CustomerViewModel { [Display(Name = "Fir

我正在一个包含复杂视图模型的项目上尝试FluentValidation,我阅读了,但我不知道如何设置规则来验证视图模型中声明的对象列表。在下面的示例中,视图模型中的列表包含一个或多个吉他对象。谢谢

查看模型

 [FluentValidation.Attributes.Validator(typeof(CustomerViewModelValidator))]
    public class CustomerViewModel
    {
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Display(Name = "Phone")]
        public string Phone { get; set; }

        [Display(Name = "Email")]
        public string EmailAddress { get; set; }

        public List<Guitar> Guitars { get; set; } 
    }
public class Guitar
{
    public string Make { get; set; }
    public string Model { get; set; }
    public int? ProductionYear { get; set; }
}
查看模型验证程序类

 public class CustomerViewModelValidator : AbstractValidator<CustomerViewModel>
    {


        public CustomerViewModelValidator()
        {
            RuleFor(x => x.FirstName).NotNull();
            RuleFor(x => x.LastName).NotNull();
            RuleFor(x => x.Phone).NotNull();
            RuleFor(x => x.EmailAddress).NotNull();
           //Expects an indexed list of Guitars here????


        }
    }
公共类CustomerViewModelValidator:AbstractValidator
{
公共CustomerViewModelValidator()
{
RuleFor(x=>x.FirstName).NotNull();
RuleFor(x=>x.LastName).NotNull();
RuleFor(x=>x.Phone).NotNull();
RuleFor(x=>x.EmailAddress).NotNull();
//这里需要一个吉他索引列表????
}
}

您可以将其添加到CustomerServiceWModelValidator中

RuleFor(x => x.Guitars).SetCollectionValidator(new GuitarValidator());
因此,您的CustomerServiceWModelValidator如下所示:

public class CustomerViewModelValidator : AbstractValidator<CustomerViewModel>
{
    public CustomerViewModelValidator()
    {
        RuleFor(x => x.FirstName).NotNull();
        RuleFor(x => x.LastName).NotNull();
        RuleFor(x => x.Phone).NotNull();
        RuleFor(x => x.EmailAddress).NotNull();
        RuleFor(x => x.Guitars).SetCollectionValidator(new GuitarValidator());
    }
}
公共类CustomerViewModelValidator:AbstractValidator
{
公共CustomerViewModelValidator()
{
RuleFor(x=>x.FirstName).NotNull();
RuleFor(x=>x.LastName).NotNull();
RuleFor(x=>x.Phone).NotNull();
RuleFor(x=>x.EmailAddress).NotNull();
RuleFor(x=>x.Guitars).SetCollectionValidator(新的GuitarValidator());
}
}
添加GuitarValidator将如下所示:

public class GuitarValidator : AbstractValidator<Guitar>
{
    public GuitarValidator()
    {
        // All your other validation rules for Guitar. eg.
        RuleFor(x => x.Make).NotNull();
    }
 }
公共类GuitarValidator:AbstractValidator
{
公共GuitarValidator()
{
//所有其他吉他验证规则。
RuleFor(x=>x.Make).NotNull();
}
}

此代码已弃用:
RuleFor(x=>x.Guitars).SetCollectionValidator(新的GuitarValidator())

这是新的:

RuleForEach(x => x.Guitars).SetValidator(new GuitarValidator());
RuleForEach(
itemToValidate=>
新的YourObjectValidator());
公共类YourObjectValidator:AbstractValidator
{
公共EdgeAppAddressValidator()
{
RuleFor(r=>r.YourProperty)
.最大长度(100);
}
}

它与最新版本的Fluent配合使用。

它与最新版本的Fluent配合使用 并包含要使用的完整示例


答案中的代码已弃用。

您不能为吉他类使用Separate验证器吗?。你想在Ilist上进行什么样的验证,是否需要有多个元素?SetCollectionValidator已被弃用,请立即使用RuleForEach。RuleForEach的语法(x=>x.Guitars)。SetValidator(new GuitarValidator());ref answer尽管此代码片段可能会回答此问题,包括解释其帮助解决问题的原因和方式可以提高答案的质量和寿命,特别是对于像这样的较老问题。看,这很有趣
RuleForEach(
  itemToValidate => 
     new YourObjectValidator());


public class YourObjectValidator : AbstractValidator<YourObject>
{
  public EdgeAPIAddressValidator()
  {
     RuleFor(r => r.YourProperty)
         .MaximumLenght(100);
  }
}