ASP.Net MVC jQuery Dialog Partial

时间:2022-11-01 11:04:46

Perhaps someone out there can help me understand what's going on. I'm using jQuery UI dialog() to display html partials in my project. When a user clicks Add New it displays the add client form. However, when the user clicks on the Add or Cancel buttons in the dialog I get an error, "$(this).dialog is not a function". If I remove the open event and display a static form in the dialog the buttons the work fine.

也许有人在那里可以帮助我理解发生了什么。我正在使用jQuery UI dialog()在我的项目中显示html部分。当用户单击“添加”时,将显示“添加客户端”表单。但是,当用户单击对话框中的“添加”或“取消”按钮时出现错误,“$(this).dialog不是函数”。如果我删除open事件并在对话框中显示静态表单按钮就可以正常工作。

ClientsController

ClientsController

public ActionResult ajaxCreateClient()
{
    Client c = new Client();
    AddToViewData(c); // adds some additional info about client
    return PartialView("__ClientForm", c);
}

View: Contacts/Create

查看:联系人/创建

....
<p>
@Html.LabelForField(model => model.Client.Name)  <!-- custom extension that I wrote -->
@Html.TextboxFor(model => model.Client.Name)
<a id="btnAddNew" href="javascript:void()">Add New</a>
</p>
....
<div id="addNew"></div>

jQuery

jQuery的

$(document).ready(function () {
    $("#btnAddNew").click(function () {
        $("#addNew").dialog("open");
    });

    $("#addNew").dialog({
        autoOpen: false,
        title: "Add Client",
        width: 410,
        modal: true,
        resizable: false,
        open: function(event, ui) {
            $(this).load("@Url.Action("ajaxCreateClient", "Clients")");
        },
        buttons:
        {
            "Add": function () {
                // validate() and do something
                $(this).dialog("close");
            },
            "Cancel": function () {
                // do something else
                $(this).dialog("close");
            }
        }
    });
});

Thanks!

谢谢!

1 个解决方案

#1


1  

Try like this:

试试这样:

$('#addNew').dialog('close');

#1


1  

Try like this:

试试这样:

$('#addNew').dialog('close');