【问题标题】:MVC 5 Password Compare always validatingMVC 5 密码比较总是验证
【发布时间】: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


【解决方案1】:
@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>

}

这工作正常。我没有看到代码有任何问题。

【讨论】:

    猜你喜欢
    • 2011-10-29
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2019-06-02
    • 2018-06-14
    • 2016-01-27
    • 2021-06-13
    • 2015-01-05
    相关资源
    最近更新 更多