1

我有一个复杂的类结构,如下所示。

public class Account
{ 
   public List<Name> Person {get;set;}
   public List<Address> Address {get;set;}
   public string Email   {get;set;}
   public string ConfirmEmail {get;set;}
}

public class Name 
{
  public string FirstName {get; set;}
  public string LastName {get;set;}
  public string DateOfBirth {get;set;}
  public string SSN {get;Set;}
}

public class Address 
{
  public string AddressLine1 {get;set;}
  public string AddressLine2 {get;set;}
  public string City {get;set;}
  public string State {get;set;}
}

这是验证器

public class AccountValidator : AbstractValidator<Account>
{
  public AccountValidator()
  {
    RuleSet("Account", () =>
    { 
     RuleFor(account => account.Person).SetCollectionValidator(new NameValidator());
     RuleFor(account => account.Address).SetCollectionValidator(new AddressValidator());
   });
     }
   }
  }


public class NameValidator : AbstractValidator<Name>
 {
    public NameValidator()
     {
        RuleSet("Account", () =>
        {
            SharedRules();
        });

        RuleSet("Name_DateOfBirth", () =>
        {
          SharedRules();
          RuleFor(name => name.DateOfBirth).NotEmpty());
        });
    }

     void SharedRules()
    {
      RuleFor(name => name.FirstName).NotEmpty());
      RuleFor(name => name.FirstName).Length(1, 20));
      RuleFor(name => name.LastName).NotEmpty());
      RuleFor(name => name.LastName).Length(1, 20));
      }
 }
public class AddressValidator : AbstractValidator<Address>
{
  public AddressValidator()
  {
    RuleSet("Account", () =>
    {
      SharedRules();
     });
  }

    void SharedRules()
    {

        RuleFor(address => address.AddressLine1).NotEmpty());
        ... 
        .... etc..
    }
 }

我有 [HttpPost] ActionMethod 如下:-

[HttpPost]
public ActionResult Register([CustomizeValidator(RuleSet="Account")] Account model)
 {
    if(MoelState.IsValid)
    {
        //blah blah
    }
    else 
    {
      //blah blah
    }
 }

我对注册的看法如下:-

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "Register" }))
{
   @Html.AntiForgeryToken();   
   <h1>Register</h1>    
   @Html.ValidationSummary(false)
   <div id="divName">   
     @Html.EditorFor(m => m.Person[0])
   </div>
      for (int i = 0; i < 2; i++)
     {
        if (i == 0)
       {  
        <div id="divHomeAdd">
            @Html.EditorFor(m => m.Address[0])
        </div>       
       <input type="checkbox"/>   
       <label for="nohomeaddress"> Do not Have Home Address </label>
       }
        if (i == 1)
      { 
        <div id="divMailingAdd">
            @Html.EditorFor(m => m.Address[1])
        </div>
        }
      }

    @Html.TextBoxCustomFor(m => m.Email)   
    @Html.TextBoxCustomFor(m => m.ConfirmEmail) 
    <input type="submit" value="Register" id="btnRegister" name="Register" />
 }

我必须显示 EditorFor() Name ,但只需要 FirstName 和 LastName ,即需要在 NameValidator 中为“Account”触发 RuelSet。

对于某些其他视图,我需要触发“Name_DateOfBirth”规则集,因为该屏幕需要出生日期作为必填字段以及正常的名字和姓氏。如何在 MVC 中做到这一点?

如果选中“无家庭地址”复选框,我必须显示家庭地址验证,然后只需要验证邮寄地址属性。

在这种情况下如何使用规则集?我们需要在 Parent 和 Child 中使用相同的规则名称吗?即“帐户”规则应该同时存在于 AccountValidator 和 NameValidator 中以便触发?

4

1 回答 1

1

到目前为止,您所拥有的似乎是一个好的开始。使用视图名称作为规则集名称是一个好主意,因为您将在视图上的哪些字段以及哪些字段得到验证之间建立良好的关系。

我注意到的唯一一件事是您缺少ValidatorAttribute模型类上的装饰。

如果您还没有看过,这里有一些很好的文档。

于 2013-06-18T21:52:02.217 回答