1

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 Answer 1

1

Try like this:

$('#addNew').dialog('close');
Sign up to request clarification or add additional context in comments.

4 Comments

Hmm, that's strange. Are you getting some javascript error? Try removing the code preceding the call to this method.
@gnome, I've tried running the example and it worked fine for me with both $(this).dialog('close'); and $('#addNew').dialog('close');. Try printing console.log($(this)); to the FireBug console to see why does it say that .dialog is an undefined function.
Well, says failed to load /Contacts/Create; which is the view in the above code snippet. Really has me stumped. Got it to work in jsfiddle, thinking it me be related to something else now.
Issue was related to something else in the partial. Appreciate the help though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.