1

是否有一种方法可以为我的属性实现必需的属性virtualPassword,但条件是否Password为 null?

这是我的代码。

[DataType(DataType.Password)]
[LocalizedDisplayName("UserPassword", NameResourceType = typeof(Languages.Names))]
[Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Languages.Validations))]        
public string Password { get; set; }

private string _virtualPassword = null;
[NotMapped]
[DataType(DataType.Password)]
[LocalizedDisplayName("UserPassword", NameResourceType = typeof(Languages.Names))]
[StringLength(12, MinimumLength = 6, ErrorMessageResourceName = "StringLengthBetween", ErrorMessageResourceType = typeof(Languages.Validations))]
public virtual string virtualPassword { get { return _virtualPassword; } set { _virtualPassword = value; } }
4

1 回答 1

0

您可以通过在模型中实现 IValidatableObject 来实现自定义验证。

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var issues = new List<ValidationResult>();
if(issue_exist_in_model) //implement whatever condition is applicable
{
    issues.Add(new ValidationResult("Message for issur that exist"));
}

  return issues;
}
于 2013-01-31T17:49:00.293 回答