错误:在初始化之前无法调用对话框上的方法;尝试拨打方法'关闭'

时间:2014-04-15 05:00:11

标签: javascript jquery html asp.net-mvc iframe

我有取消按钮,在JQuery模式对话框上无法正常工作,我真的很感激一些帮助。这是我点击模态的取消按钮时遇到的例外:JavaScript运行时错误:在初始化之前无法调用对话框上的方法;试图调用方法'关闭'

这是我在模态中显示的局部视图:

@model Models.Office
@{ 
    Layout = null;
}
@Scripts.Render("~/bundles/query-1.8.2.js")
@Scripts.Render("~/bundles/jquery-ui.js")
@Styles.Render("~/Content/bootstrap")
@Scripts.Render("~/bundles/bootstrap")
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/themes/base/css")
@Scripts.Render("~/bundles/modernizr")
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

<script type="text/javascript">
    function CloseModal() {
        $(this).dialog("close");
    }
</script>

@*@using (Ajax.BeginForm("EditOffice", "Admin", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "edit-Dialog" }))*@
@using (Html.BeginForm("CreateEditOffice", "Admin", "POST"))
{
    <fieldset>
        <legend>Office</legend>
        @if (ViewBag.IsUpdate == true)
        {
            @Html.HiddenFor(model => model.OfficeId)
        }

        <div class="display-label">
            @Html.DisplayNameFor(model => model.OfficeName)
        </div>
        <div class="Input-medium">
            @Html.EditorFor(model => model.OfficeName)
            @Html.ValidationMessageFor(model => Model.OfficeName)
        </div>

        <div class="display-label">
            @Html.DisplayNameFor(model => model.SupportNo)
        </div>
        <div class="Input-medium">
            @Html.EditorFor(model => model.SupportNo)
            @Html.ValidationMessageFor(model => model.SupportNo)
        </div>

        <div class="display-label">
            @Html.DisplayNameFor(model => model.SupportEmail)
        </div>
        <div class="Input-medium">
            @Html.EditorFor(model => model.SupportEmail)
            @Html.ValidationMessageFor(model => model.SupportEmail)
        </div>
        <div class="display-label">
            @Html.DisplayNameFor(model => model.SupportFax)
        </div>
        <div class="Input-medium">
            @Html.EditorFor(model => model.SupportFax)
            @Html.ValidationMessageFor(model => model.SupportFax)
        </div>

        <div class="display-label">
            @Html.DisplayNameFor(model => model.SupportFtp)
        </div>
        <div class="Input-medium">
            @Html.EditorFor(model => model.SupportFtp)
            @Html.ValidationMessageFor(model => model.SupportFtp)
        </div>
    </fieldset>
    <br />
    <div class="pull-right">
        @if (ViewBag.IsUpdate == true)
        {
            <button class="btn btn-primary" type="submit" id="btnUpdate" name="Command" value ="Update">Update</button>
        }
        else
        {
            <button class="btn btn-primary" type="submit" id="btnSave" name="Command" value="Save">Save</button>
        }
            <button type="button" id="Cancel" class="btn btn-primary" onclick="CloseModal()">Cancel</button>
    </div>
}

//Then this is the scrip on the parent view:

<script type="text/javascript">
    $(document).ready(function () {
        $(".editDialog").on("click", function (e) {
            // e.preventDefault(); use this or return false
            var url = $(this).attr('href');
            $("#dialog-edit").dialog({
                title: 'Edit Office',
                autoOpen: false,
                resizable: false,
                height: 450,
                width: 380,
                show: { effect: 'drop', direction: "up" },
                modal: true,
                draggable: true,
                open: function (event, ui) {
                    $(this).load(url);
                },
                close: function (event, ui) {
                    $("#dialog-edit").dialog().dialog('close');
                }
            });

            $("#dialog-edit").dialog('open');
            return false;
        });
    });  
</script>

我做错了什么???? 此外,当我删除上面的jquery文件的引用时,它的工作完美。

1 个答案:

答案 0 :(得分:0)

尝试使用下面的

修改关闭按钮事件
Close": function () {
                        $('#AddUserForm').dialog('close');
                        return false;
                    }