2

所以我有一个正在注册用户和业务合作伙伴的网络应用程序。

这就是我的模型的样子

public class UserModel : IValidatableObject
{
   //here are some properties and methods that I am using in Validate method        

    public CompanyModel Company { get; set; } //this is user Company

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {       
         if (this.UserWithEmailExists())
             yield return new ValidationResult("Email already exists", new[] { "Email" });

         if (this.UserWithUsernameExists())
             yield return new ValidationResult("Username already exists", new[] { "Username" });
    }
}

public class CompanyModel : IValidatableObject
{
    //again here are some properties that i am using in Validate

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (this.Phone == null && this.Mobile == null)
            yield return new ValidationResult("The Phone field is required", new[] { "Phone" });
    }
}

//this is the model passed to the view
public sealed class RegistrationModel : UserModel, IValidatableObject
{
    public new IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (this.Password != this.PasswordValidation)
            yield return new ValidationResult("Ponovite prejšnji vnos", new[] { "PasswordValidation" });
    }
}

问题是:当我提交表单时,CompanyModel 中的 Validate 被调用,并且 RegistrationModel 中的 Validate 也被调用。但我也想在 UserModel 中调用 Validate...

更新:我解决了第一个问题:这些是升级后的模型:)

public class UserModel : IValidatableObject
{
   //here are some properties and methods that I am using in Validate method        

    public CompanyModel Company { get; set; } //this is user Company

    public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {       
         if (this.UserWithEmailExists())
             yield return new ValidationResult("Email already exists", new[] { "Email" });

         if (this.UserWithUsernameExists())
             yield return new ValidationResult("Username already exists", new[] { "Username" });
    }
}

public class CompanyModel : IValidatableObject
{
    //again here are some properties that i am using in Validate

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (this.Phone == null && this.Mobile == null)
            yield return new ValidationResult("The Phone field is required", new[] { "Phone" });
    }
}

//this is the model passed to the view
public sealed class RegistrationModel : UserModel, IValidatableObject
{
    public override new IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        //this is needed so that UserModel validation executes
        foreach (var result in base.Validate(validationContext))
        {
            yield return result;
        }

        if (this.Password != this.PasswordValidation)
            yield return new ValidationResult("Ponovite prejšnji vnos", new[] { "PasswordValidation" });
    }
}

现在验证工作正常。我只是不知道为什么 CompanyModel 中的 Validate 执行两次???。有什么建议吗?

4

1 回答 1

6

当您覆盖基类中的方法时,不会显式调用该基类中的方法。您需要自己做,所以在RegistrationModelValidate的函数中,添加对UserModel基类中的函数的调用。Validate

base.Validate(validationContext);

但是,正如您yield return在验证器中使用的那样,这使Validate函数成为迭代器,因此必须对其进行迭代才能使一切正常工作。根据这个问题,完整的解决方案是在您的RegistrationModel.Validate函数中使用此代码:

foreach (var result in base.Validate(validationContext))
{
    yield return result;
}
于 2013-04-17T17:17:58.117 回答