表单上的MVC提交打开JQuery对话框,等待用户响应

MVC on form submit open JQuery dialog and wait for user response

本文关键字:对话框 等待 用户 响应 JQuery MVC 提交 表单      更新时间:2023-09-26

我有MVC应用程序;在表单内部,当用户单击"保存"按钮时,它应该将表单提交给控制器并保存更改。如果满足某些条件,它将弹出JQuery UI对话框,执行一些ajax调用,要求用户在对话框中输入,并在用户单击对话框中的新"保存"按钮后提交表单。

我的问题是,在表单"保存"按钮单击,对话框出现,但页面立即发布(我可以看到加载器在浏览器选项卡窗口中工作),用户永远不会与对话框交互,因为它在成功的页面发布后消失。我的HTML:

@using (Html.BeginForm("Edit", "LsaPage", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    //some other fields
    <input type="submit" name="actionType" value="Save" id="btnFormSubmit"/>
<div id="inctivePageDialog" title="This is JQuery dialog">
    <p> Do you wish to terminate LSA Page on Grids?</p>
    <div id="termdatebox">
        <h5>Enter Page termination date.</h5> 
        <input class = "datepicker" id="pageTermDate"/>
        <button type="submit" id="finalize" value="Save">Save</button>
    </div>
</div>
}

和JQuery:

$('#btnFormSubmit').click(function (e) { //form submit button
    if (newPageStatus == "I" && pageStatus == "A") { //conditions
        $.ajax({
            type: 'GET',
            url: '@Url.Action("InactivatePage", "LsaPage")',
            data: { pageId: "@Model.PageID" },
            dataType: 'json',
            cache: false,
            async: false,
            success: function (data) {
                var anyActive = 0;
                $.each(data, function (index, item) {
                    //processing
                    anyActive += 1;
                });
                if (anyActive > 0) {
                    $('#inctivePageDialog').dialog("open"); //open the jquery dialog
                }
            }
        });
    }
});
//dialog that shows up when conditions are met
$('#inctivePageDialog').dialog({
    autoOpen: false,
    modal: true,
    width: 500,
    height: 500,
    buttons: {
        Yes: function () {
            //if user clicks yes on the dialog, it will ask for the date and show new Save button
            $('#termdatebox').show(); //show input textbox
            $('.ui-button:contains("Yes")').hide(); //hide original dialog Yes button
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    }
});
//Save button click event on the dialog 
$('#finalize').on('click', function (e) {
    var termdate = $('#pageTermDate').val();//get user input
    $('#inctivePageDialog').dialog('close');
    $.ajax({
        type: 'POST',
        url: '@Url.Action("InactivatePageRelations", "LsaPage")',
        data: { pageId: "@Model.PageID", termDt: termdate },
        dataType: 'json',
        cache: false,
        async: false,
        success: function (data) {
            debugger
            $('form').submit();
        }
    });
});

你应该把e.p preventdefault()作为$('#btnFormSubmit')的第一行代码。点击(function (e){…})它会阻止表单提交事件的执行,这样你就可以执行ajax调用和逻辑

有关调用的更多信息,请查看

您自己通过Ajax发布表单。按钮类型应该是type="button",这将防止表单的post返回。

<button type="button" ...>Save</button>
此外,你不应该同时使用ajaxsuccess:…$('form').submit();;这说不通啊。