The jQuery UI dialog box will not close

advertisements

Given the following script:

$(function () {
    $(".editLink").button();

    $('#editPersonDialog').dialog({
        autoOpen: false,
        width: 800,
        resizable: false,
        title: 'Edit Person',
        modal: true,
        buttons: {
            "Save": function () {
                $("#update-message").html('');
                $("#updatePersonForm").submit();
            },
            "Close": function () {
                $(this).dialog('close');
            }
        },
        close: function (event, ui) {
            $(this).dialog('close');
        }
    });

    $(".editLink").click(function () {
        var dialogDiv = $('#editPersonDialog');
        var linkObj = $(this);
        var viewUrl = linkObj.attr('href');
        $.get(viewUrl, function (data) {
            dialogDiv.html(data);
            //validation
            var $form = $("#updatePersonForm");
            // unbind existing validation
            $form.unbind();
            $form.data("validator", null);
            // check document for changes
            $.validator.unobtrusive.parse(document);
            // re-add validation with changes
            $form.validate($form.data("unobtrusiveValidation").options);
            // open dialog
            dialogDiv.dialog('open');
        });

        return false;
    });
});

function updateSuccess() {
    if ($("#update-message").html() == "True") {
        $('#editPersonDialog').dialog('close');
        $("#commonMessage").html("Update Complete");
        $("#commonMessage").delay(400).slideDown(400).delay(3000).slideUp(400);
    }
    else {
        $("#update-message").show();
    }
}

If I click the "X" button on the dialog the form closes fine. If I click the "Close" button then it does not close. I have verified that the code for the "Close" button is being called.

Both the "X" button and the "Close" button are both running the same statement: '$(this).dialog('close');'. Why would one work and the other not work?

As an aside the dialog will not open a second time unless I refresh the page. I imagine that these 2 problems may be related.

I have found many people with similar problems and a number of different solutions that worked for them. Unfortunately none of them worked for me.

Further Info:

The dialog displays a partial view in an Ajax form:

@using (Ajax.BeginForm("Edit", "Person", null,
    new AjaxOptions
    {
        UpdateTargetId = "update-message",
        InsertionMode = InsertionMode.Replace,
        HttpMethod = "POST",
        OnSuccess = "updateSuccess"
    },
    new { @id = "updatePersonForm" }))
{
    @Html.ValidationSummary(true)
    <div id="update-message"  class="hiddenDiv"></div>
    <div class="blockGraygradient">
        @Html.Partial("_CreateEditCommon")
        @Html.HiddenFor(model => model.SelectedPerson.Id)
        @Html.HiddenFor(model => model.SelectedPerson.RowVersion)
        @Html.HiddenFor(model => model.SelectedPerson.CreateTime)
    </div><p/>
}


Try using this instead.

$(this).dialog('destroy');

We had exactly the same problem. In the end, every time you re-opened the dialog it was actually re-injecting the dialog markup into the DOM. Then, when you click close (for the second time) it only closes the first occurrence of the dialog, but not necessarily the one that's open. You can check this using a run-time DOM inspector like FireBug or Chrome's built-in developer tools.