MVC OnSuccess脚本错误

时间:2014-01-30 07:03:47

标签: jquery asp.net-mvc asp.net-mvc-4

我的父表格有以下链接:

@Ajax.ActionLink("Delete", "ControllerAction", new { id=Model.Id }, new AjaxOptions
                       {
                           HttpMethod = "GET",
                           UpdateTargetId = "targetDiv",
                           InsertionMode = InsertionMode.Replace,                           
                           OnSuccess = "openPopup()"
                       })

OpenPopup脚本函数:

function openPopup() {
        $('#test').dialog({
            autoOpen: true,
            width: 800,
            resizable: false,               
            modal: true,
            open: function (event, ui) {
            }
        });
    }

在我的部分视图中,我有Ajax.BeginForm

@using (Ajax.BeginForm("ActionGoesHere", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "selectedview", LoadingElementId = "spinner", InsertionMode = InsertionMode.Replace, OnSuccess = "CloseWindow" })) {....

}

<script type="text/javascript">    

    function CloseWindow() {
        $('#test').dialog("close");
    }



</script>

成功操作后,它会关闭对话框窗口。但是我收到以下脚本错误。

Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'close'

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

首先应该初始化对话框意味着打开对话框然后调用close。你只需要调用这个序列,

openPopup() --->  CloseWindow()

这里的代码是

$(function() {
    function openPopup() {
        $('#dialog').dialog({
            autoOpen: true,
            resizable: false,               
            modal: true,
            open: function (event, ui) {
            }
        });
    }
    function CloseWindow() {
        $('#dialog').dialog("close");
    }


    openPopup()
    CloseWindow() //first run comment it and second run remove comment

  });

<强> HTML:

<div id="dialog" title="Basic dialog">
  <p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>

演示链接http://jsfiddle.net/dhana36/6TssK/1/