.NET MVC和;模态对话框中的jQuery-UI表单

时间:2013-02-04 21:11:31

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

我正在撕扯我的头发试图找出我遇到这个问题的原因。我正在使用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>

Javascript初始化对话框并填充inner-div:

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的技术无济于事。

1 个答案:

答案 0 :(得分:2)

你不需要Html.EndForm();这是在html输出结束时生成第二个</form>。另请检查您的部分视图中是否未返回任何JavaScript库。