MVC 5密码比较总是在验证

时间:2017-03-28 02:57:59

标签: asp.net-mvc asp.net-mvc-5 data-annotations

我是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; }

这是我在View中的代码

<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>

1 个答案:

答案 0 :(得分:0)

@model FailTracker.Models.LoginUser //your model

@{
ViewBag.Title = "Test";
}

@using(Html.BeginForm())    {

// your  view code goes here 

<div class="form-group">
<button type="submit" class="btn btn-success">Submit</button>
</div>

}

这很好用。我没有看到代码有任何问题。