bound event handler not firing modal dialog button

2019-01-29 10:56发布

问题:

I have a scenario that to me sounds just like this SO post and others who have asked similar....only I'm not understanding what others have gotten from these post as my implementation is partially failing.

Basically I have a page with a button for adding a new record. Click add button and a modal dialog opens. The modal dialog is a different view than the parent page with of course the fields needed for creating the record.

Clicking save (remember save is a button on the modal I want my save event to fire but it never does...nor do I get any errors reported in dev tools or fiddler. Here is the code for launching the modal and in the comments you will see how far I get.

$(document).ready(function ()
{
    $("#new").click(function (e) //attach new button works great
    {
        e.preventDefault(); //seems to work
        var ischanging = false; //hard coded vars for testing
        var financierid = 0;

        var detailsWindow = $("#window").data("kendoWindow");//handle if existing window

        if (!detailsWindow)
        {
            // create a new window, if there is none on the page
            detailsWindow = $("#window")
                // set its content to 'loading...' until the partial is loaded
                .html("Loading...")
                .kendoWindow(
                    {
                        ....removed options for brevity
                    })
                .data().kendoWindow.bind('refresh', function (e)//attach refresh
                {
                    alert('bound'); //this alert displays after modal loads
                    $('#save').click(function (event)
                    {
                        alert('dqewewr'); 
                        //this alert never fires nor anything subsequent

                        event.preventDefault();
                        // take over and perform ajax post

                        alert('parent');

                        $.ajax(
                        {
                            type: "POST",
                            url: "@Html.Raw(Url.Action("CreateEdit", "Finance"))",
                            data: $(this).serialize(),
                            success: function (data) {
                            //here we check if called resulted in Success/Failure
                            if (data.Result === "Success") 
                               {
                                    alert('Finis');
                                }
                                else {
                                    //Show error message or whatever.
                                }
                            }
                        })
                    });
                }).center();
        }

        detailsWindow.open();


    });
});

My modal is NOT wrapped in a form tag...didn't think I would need it if I'm using an ajax post...?

<span id="save" class="k-button" style="margin-right:10px;">Save</span>

Finally just to make sure I am clear. My parent page is Configure.cshtml and my modal is CreateEdit.cshtml....in other words while modal is being generated we hit controller which does it's work then returns the CreateEdit view populated with model accordingly.

This has frustrated me all week and while I have been able to work on other items in between I am now coming to a head where this is one of the last things before completion of this task.

Thank you very much for any help.


Stephen Muecke...if you see this change your comments to answer and I will mark as that is what solved the problem for me.