如何使用Fluent验证来验证列表中的每个字符串?我有一个MVC3视图模型定义为:[Validator(typeof(AccountsValidator))]publicclassAccountViewModel{publicListAccounts{get;{publicAccountsValidator(){RuleFor(x=>x.Accounts).SetCollectionValidator(newAccountValidator());//这行不通}}并且可以定义帐户验证:publicclassAccountValidator:AbstractValidator{publicOrderValidator(){RuleFor(x=>x).NotNull();//这里有任何其他验证}}我希望列表中的每个帐户都按照文档中的描述进行修改。但是,对SetCollectionValidator的调用不起作用,因为在使用List时这不是一个选项,尽管如果它被定义为一个List,该选项就在那里。我错过了什么吗?我可以更改我的模型以使用List,然后定义Account类,但我真的不想更改我的模型以适应验证。作为参考,这是我正在使用的图片:@modelMvcApplication9.Models.AccountViewModel@using(Html.BeginForm()){@*第一个账号是必填字段。*@账号*@Html.EditorFor(m=>m.Accounts[0].Account)@Html.ValidationMessageFor(m=>m.Accounts[0].Account)for(inti=1;im.Accounts[i].Account)@Html.ValidationMessageFor(m=>m.Accounts[i].Account)}}以下应有效:publicclassAccountsValidator:AbstractValidator{publicAccountsValidator(){RuleFor(x=>x.Accounts).SetCollectionValidator(newAccountValidator("Accounts"));}}publicclassAccountValidator:AbstractValidator{publicAccountValidator(stringcollectionName){RuleFor(x=>x).NotEmpty().OverridePropertyName(collectionName);}}验证类:usingFluentValidation;使用System.Collections.Generic;命名空间Test.Validator{公共类EmailCollection{公共IEnumerable电子邮件{得到;放;}}publicclassEmailValidator:AbstractValidator{publicEmailValidator(){RuleFor(x=>x).Length(0,5);}}publicclassEmailListValidator:AbstractValidator{publicEmailListValidator(){RuleFor(x=>x.email).SetCollectionValidator(newEmailValidator());}}}注:我使用最新的MVC5(Nuget)版本的fluentvalidation来尝试使用:以上是C#学习教程:HowtouseFluentvalidationforeachcharacterintheliststringforvalidation?如果分享的内容对你有用,需要进一步了解C#学习教程,希望你多多关注——publicclassAccountsValidator:AbstractValidator{publicAccountsValidator(){RuleForEach(x=>x.Accounts).NotNull()}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: