1

我一直试图弄清楚这一点,但没有运气。我有一个具有两个属性的模型:PasswordConfirmPassword.

如果我在视图中将它们显示为 TextBoxes ( Html.TextBoxFor(...)),则验证工作正常。但是,如果我将这些显示为密码字段 ( Html.PasswordFor(...)),那么在提交表单之前验证将不起作用。

下面是我的视图和模型的代码。请注意,该视图只是一个测试视图,我没有在其上放置提交按钮。模型:

public class TestViewModel
{
    [Required(ErrorMessage = "Password is required")]
    [StringLength(40, ErrorMessageResourceType = typeof(Resources.Shared_Editor_SecurityDetails), ErrorMessageResourceName = "PasswordLength")]
    public string Password { get; set; }

    [Required(ErrorMessage = "Confirm is required")]
    [Compare("Password", ErrorMessage = "Must be the same as Password")]
    public string ConfirmPassword { get; set; }
}

看法:

@using (Html.BeginForm())
{
<div>
    @Html.LabelFor(m => m.Password)
    @Html.PasswordFor(m => m.Password)
    @Html.ValidationMessageFor(m => m.Password)
</div>

<div>
    @Html.LabelFor(m => m.ConfirmPassword)
    @Html.PasswordFor(m => m.ConfirmPassword)
    @Html.ValidationMessageFor(m => m.ConfirmPassword)
</div>
}

如果有人能够提供帮助,那将不胜感激。

非常感谢!

4

1 回答 1

1

Although we didn't get to the root cause of this issue, we have come up with a workaround.

We set a function to be triggered on blur() for each of the password fields using jquery and the field id's. In this function we perform a $(this).valid() or $('#idOfField').valid().

Equally you could perform this in the keyUp() function too.

This is forcing the validation but if anybody can come up with an actual reason as to why we may have encountered this issue I would be very intrigued to hear it.

于 2013-04-18T16:25:08.150 回答