3

我正在努力弄清楚为什么会遇到这个问题。我使用 jQuery-ui 模型对话框作为更新数据的弹出表单。在我尝试在包含实际表单的对话框 div 中放置一个 div 之前,这工作得很好而且很花哨。打开模式工作正常,但是当我按下“取消”按钮时,我在 javascript 控制台中收到此错误:

Uncaught TypeError: Object [object Object] has no method 'dialog'

此外,窗户永远不会关闭。最后,如果我按下模态框右上角的“X”按钮,窗口会正常关闭,但是当我再次尝试打开模态框时,我会得到同样的错误。

以下是相关代码:

视图中的 div:

<div id="TacticInstanceModal" title="Update Tactic">
    <div id="addEditTacticsInstanceContent">
    </div>
</div>

用于初始化对话框并填充内部 div 的 Javascript:

var addEditTacticInstance = '@Url.Action("Edit", "TacticInstance")';
$('#updateTacticInstanceButton').live("click", function () {
    $('#TacticInstanceModal').dialog({
        resizable: false,
        modal: true,
        buttons: {
            Save: function () {
                $(this).dialog("close");
                SaveTacticInstance();
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        }
    });

    $.get(addEditTacticInstance,
        { id: $(this).parent().parent().find('.tacticInstanceIdField').val() },
        function (data) {
            $('#addEditTacticsInstanceContent').html(data);
        });
});

加载部分视图的操作:

public ActionResult Edit(int id = 0)
    {
        try
        {
            TacticInstance ti = null;

            if (id != 0)
            {
                ti = tacticInstanceRepository.GetSingle(x => x.TacticInstanceId == id);
                ti.Tactic = tacticRepository.GetSingle(t => t.TacticId == ti.TacticId);
            }
            else
            {
                ti = new TacticInstance();
                ti.TacticInstanceId = 0;
            }

            TacticInstanceViewModel tacticInstance = new TacticInstanceViewModel
            {
                TacticId = ti.TacticId,
                TacticInstanceId = ti.TacticInstanceId,
                StartDate = ti.StartDate,
                EndDate = ti.EndDate,
                CompletedDate = ti.CompletedDate,
                PropertyEnrollmentId = ti.PropertyEnrollmentId,
                TacticName = ti.Tactic.Name

                //TODO: Implement Assets
            };

            return PartialView("_AddEdit", tacticInstance);
        }
        catch (EntityException e)
        {
            ErrorSignal.FromCurrentContext().Raise(e);
            return View("Error");
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            return View("Error");
        }
    }

局部视图:

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    @Html.Hidden("TacticInstance_TacticId", @Model.TacticId)
    @Html.Hidden("TacticInstance_InstanceId", @Model.TacticInstanceId)
    @Html.Hidden("TacticInstance_PropertyEnrollmentId", @Model.PropertyEnrollmentId)

   <div class="editor-label">
        @Html.LabelFor(model => model.TacticName, new { @style= "font-weight:bold" } )
    </div>
    <div class="editor-field">
        @Html.Label("TacticInstance_Name", Model.TacticName.ToString(), new { id = "TacticInstance_Name" })
    </div>

    <div class="editor-label">
       @Html.LabelFor(model => model.StartDate, new { @style= "font-weight:bold" } )
    </div>
    <div class="editor-field">
        @Html.Label("TacticInstance_StartDate", String.Format("{0:MM/dd/yyyy}", Model.StartDate), new { id = "TacticInstance_StartDate" })
    </div>

    <div class="editor-label">
          @Html.LabelFor(model => model.EndDate, new { @style= "font-weight:bold" } )
    </div>
    <div class="editor-field">
        @Html.Label("TacticInstance_EndDate", String.Format("{0:MM/dd/yyyy}", Model.EndDate), new { id = "TacticInstance_EndDate" })
    </div>
    <div class="editor-label">
         @Html.LabelFor(model => model.CompletedDate, new { @style= "font-weight:bold" } )
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.CompletedDate, new { id = "TacticInstance_CompletedDate" })
        @Html.ValidationMessageFor(model => model.CompletedDate)
    </div>
Html.EndForm();
}

最奇怪的是,如果我在 javascript 中注释掉 ajax .get 但将生成的部分视图 HTML 复制/粘贴到视图上的 div 中,它就可以正常工作。这让我相信问题出在某个地方,但我尝试了多种注入 html 的技术,但无济于事。

4

1 回答 1

2

你不需要 Html.EndForm(); </form>这会在 html 输出的末尾产生一秒钟。还要检查您的部分视图中是否没有返回任何 javascript 库。

于 2013-02-04T22:05:42.867 回答