ASP.Net MVC jQuery Dialog Partial

2019-08-02 18:52发布

问题:

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.

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

$(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:

Try like this:

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