0

我已经完全按照 Darin Dimitrov 在以下位置提供的 NotEqual 属性实现了: https ://stackoverflow.com/a/5742494/1946707

我在这里遇到的问题是我正在使用一个 jQuery 对话框,并且希望在我的对话框的客户端获得验证。我需要的验证触发,但不相等的验证在客户端不起作用......任何帮助将不胜感激。

这是我的模型:

public class ChangePasswordModel
{
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Current password")]
    public string OldPassword { get; set; }

    [Required]
    [NotEqual("OldPassword", ErrorMessage = "should be different than Prop1")]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "New password")]
    public string NewPassword { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm new password")]
    [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

这是我的看法:

@using (Html.BeginForm("ChangePassword", "Account", new { area = "" }, FormMethod.Post, new { id = "changePasswordForm" }))
{

    <a href="#" onclick="openChangePasswordDialog();">Change Password</a>
    <div id="dialogChangePassword" title="Change Password">
        <table>
            <tr>
                <td>@Html.LabelFor(m => m.OldPassword)
                </td>
                <td>@Html.PasswordFor(m => m.OldPassword)
                </td>
                <td>@Html.ValidationMessageFor(m => m.OldPassword)
                </td>
            </tr>
            <tr>
                <td>@Html.LabelFor(m => m.NewPassword)
                </td>
                <td>@Html.PasswordFor(m => m.NewPassword)
                </td>
                <td>@Html.ValidationMessageFor(m => m.NewPassword)
                </td>
            </tr>
            <tr>
                <td>@Html.LabelFor(m => m.ConfirmPassword)
                </td>
                <td>@Html.PasswordFor(m => m.ConfirmPassword)
                </td>
                <td>@Html.ValidationMessageFor(m => m.ConfirmPassword)
                </td>
            </tr>
        </table>
    </div>
}

这是我的javascript:

    var changePasswordDialogId = '#dialogChangePassword';
    var oldPasswordInput = '#changePasswordForm #OldPassword';
    var newPasswordInput = '#changePasswordForm #NewPassword';
    var confirmPasswordInput = '#changePasswordForm #ConfirmPassword';

    $(document).ready(function () {
        $(changePasswordDialogId).dialog(
    {
        autoOpen: false,
        autoResize: true,
        modal: true,
        open: function () {
            $(this).parent().appendTo("#changePasswordForm");
        },
        buttons: {
            Ok: function () {
                if ($(oldPasswordInput).valid() && $(newPasswordInput).valid() && $(confirmPasswordInput).valid()) {
                    alert('ready to post!');
                    $(changePasswordDialogId).dialog('close');
                }
            },
            Cancel: function () {
                $(changePasswordDialogId).dialog('close');
            }
        }
    });
});       //document.ready

function openChangePasswordDialog() {
    $(changePasswordDialogId).dialog('open');
}
4

2 回答 2

0

似乎您忘记注册您的验证适配器

jQuery.validator.unobtrusive.adapters.add(
        'notequalto', ['other'], function (options) {
            options.rules['notEqualTo'] = '#' + options.params.other;
            if (options.message) {
                options.messages['notEqualTo'] = options.message;
            }
    });

达林·季米特洛夫(Darin Dimitrov)在他的回答中具体说明了这一点。

于 2013-07-29T15:33:06.593 回答
0

好的,所以我发现了我的问题。IClientValidatable 我错过了从现在添加它的继承,它可以在客户端工作,即使在我的 jQuery 对话框中也是如此。

于 2013-07-29T16:13:40.303 回答