1

I have the following partial view (I have removed some of the formatting just to keep it simple on this forum). My form tag is on the parent page, and the items on the dialog are a part of that form. This is a strongly typed partial view. I have defined the password and confirmPassword fields as [Required] in my Model.

@Html.LabelFor(m => m.password)
<a href="#" onclick="openResetPasswordDialog();">Reset Password</a>

<div id="dialogResetPassword" title="Reset Password">
    <p>Reset password for user: @Model.userId</p>
        @Html.LabelFor(m => m.password)
        @Html.PasswordFor(m => m.password)
        @Html.ValidationMessageFor(m => m.password)
    <div class="rowEnd"></div>

    @Html.LabelFor(m => m.confirmPassword)
    @Html.PasswordFor(m => m.confirmPassword)
    @Html.ValidationMessageFor(m => m.confirmPassword)
    <div class="rowEnd"></div>
</div>

I have the following javascript for the initialization of the dialog:

function initializeResetPasswordDialog() {
    $(resetPasswordDialogId).dialog(
    {
        autoOpen: false,
        autoResize: true,
        buttons: {
            Ok: function () {
                if (!($('#userForm').valid())) {
                    return false;
                }
                //more code goes here ...
                closeResetPasswordDialog();
            },
            Cancel: function () {
                closeResetPasswordDialog();
            }
        }
    });
}

My dialog initializes, opens and closes fine, but when I try to check the validity of the items in the dialog, I always get "valid".

I do not want to submit the entire form, but just submit the password and the confirm password fields on the Ok button, and also fire up my validations. Any suggestions on how to do this?

4

1 回答 1

0

好的,所以我通过将以下函数添加到 jQuery 对话框的 open 参数来解决它:

        open: function () {
            $(this).parent().appendTo("#userForm");
        }

我注意到,在初始化 jQuery 对话框时,即使它在我的表单中,当 HTML 页面在浏览器中呈现时,对话框 div 也被移到了表单之外。为了使验证生效,输入必须在表单中。

我希望这篇文章也能帮助其他人!

于 2013-06-13T15:17:58.163 回答