Wie validieren Sie mit Fluent Validation für jede Zeichenfolge in einer Liste?

Ich habe ein MVC3-Ansichtsmodell definiert als:

<code>[Validator(typeof(AccountsValidator))]
public class AccountViewModel
{
    public List<string> Accounts { get; set; }
}
</code>

Mit der mit FluentValidation (v3.3.1.0) definierten Validierung als:

<code>public class AccountsValidator : AbstractValidator<AccountViewModel>
{
    public AccountsValidator()
    {
        RuleFor(x => x.Accounts).SetCollectionValidator(new AccountValidator()); //This won't work
    }
}
</code>

Und die Kontoüberprüfung wäre möglicherweise definiert:

<code>public class AccountValidator : AbstractValidator<string> {
    public OrderValidator() {
        RuleFor(x => x).NotNull();
        //any other validation here
    }
}
</code>

Ich möchte, dass jedes Konto in der Liste wie in der Tabelle beschrieben bewertet wirdDokumentation. Allerdings ist der Aufruf anSetCollectionValidator funktioniert nicht, da dies bei Verwendung von a keine Option istList<string> obwohl die Option dort wäre, wenn sie als definiert wäreList<Account>. Vermisse ich etwas? Ich könnte mein Modell ändern, um zu verwendenList<Account> und dann eine Konto-Klasse definieren, aber ich möchte mein Modell nicht wirklich ändern, um es der Validierung anzupassen.

Als Referenz ist dies die Ansicht, die ich benutze:

<code>@model MvcApplication9.Models.AccountViewModel

@using (Html.BeginForm())
{
    @*The first account number is a required field.*@
    <li>Account number* @Html.EditorFor(m => m.Accounts[0].Account) @Html.ValidationMessageFor(m => m.Accounts[0].Account)</li>

    for (int i = 1; i < Model.Accounts.Count; i++)
    {
        <li>Account number @Html.EditorFor(m => m.Accounts[i].Account) @Html.ValidationMessageFor(m => m.Accounts[i].Account)</li>
    }

    <input type="submit" value="Add more..." name="add"/>
    <input type="submit" value="Continue" name="next"/>
}
</code>

Antworten auf die Frage(3)

Ihre Antwort auf die Frage