【发布时间】:2017-03-28 02:57:58
【问题描述】:
我是 MVC 5 的新手,我的问题是每次我尝试在“确认密码”字段中输入相同的密码时,它总是会触发验证消息 [Compare]。这是个常见的问题吗 ?
这是我在模型中的代码:
[Required]
[StringLength(MaxUserNameLength)]
[Display(Name = "Username")]
public virtual string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[StringLength(MaxPasswordLength)]
public virtual string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "Doesn't Match")]
public string ConfirmPassword { get; set; }
这是我在视图中的代码
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control required" })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control required" })
@Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
</div>
</div>
【问题讨论】:
-
您能否也显示您提交表单的操作?
-
有两个 CompareAttribute 类 - 你知道你使用的是哪一个吗?
-
@TiesonT。他们的工作方式不一样吗?
-
@Usman 验证在我关注比较密码后触发
-
它们在不同的命名空间中。 System.ComponentModel.DataAnnotations.CompareAttribute 和 System.Web.Mvc.CompareAttribute。我必须检查源代码,但我似乎记得后者有一些问题。
标签: asp.net-mvc asp.net-mvc-5 data-annotations