asp.net mvc的Model验证问题
类Register:publicclassRegister{[Key]publicintId{get;set;}[Required][StringLength(100,E...
类Register:
public class Register
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "确认密码")]
[Compare("Password",ErrorMessage = "两次输入的密码不一致")]
[NotMapped]
public string ConfirmPassword { get; set; }
public virtual Member Member { get; set; }
public virtual MemberInfo MemberInfoes { get; set; }
}
登录时不需要指定ConfirmPassword,注册时需要,但登录时出现ModelState验证失败,因为没有输入ConfirmPassword的文本框,也不需要,怎么协调登录和注册
试了试登录时在Controller中为ConfirmPassword赋值,赋值为Password的值,ModelState还是验证失败,提示“两次输入的密码不一致”
用Bind(Exclude="ConfirmPassword")也不行 展开
public class Register
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "确认密码")]
[Compare("Password",ErrorMessage = "两次输入的密码不一致")]
[NotMapped]
public string ConfirmPassword { get; set; }
public virtual Member Member { get; set; }
public virtual MemberInfo MemberInfoes { get; set; }
}
登录时不需要指定ConfirmPassword,注册时需要,但登录时出现ModelState验证失败,因为没有输入ConfirmPassword的文本框,也不需要,怎么协调登录和注册
试了试登录时在Controller中为ConfirmPassword赋值,赋值为Password的值,ModelState还是验证失败,提示“两次输入的密码不一致”
用Bind(Exclude="ConfirmPassword")也不行 展开
1个回答
展开全部
这种情况不要用特性验证了,使用IValidatableObject接口吧。
模型实现IValidatableObject接口,然后在里面实现就行了。比如我想来个日期比较
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (this._AnnouncementDateEnd < this._AnnouncementDateBegin)
{
yield return new ValidationResult("截止日期必须大于开始日期!");
}
}
更多追问追答
追问
这是新建一个自定义的Attribute验证特性吗
追答
不是,是接口
public class Register : IValidatableObject
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询