MVC 表单未从 JQuery 模式内部提交

发布于 2025-01-03 21:28:22 字数 4254 浏览 0 评论 0原文

我在 MVC 中提交的表单时遇到问题。它可以从两个不同的点加载,一个是它提交的点,另一个是它不回发的点。

它是在 JQuery 模式对话框内加载的部分视图。当我从主页触发模式时,它将正常提交。

还有另一个模式可以启动以允许用户找到任务,然后允许启动第二个模式来显示表单。当提交相同的表单时,它不会触发向控制器发送帖子。

这是 JQuery 脚本:

$(document).ready(function () {
    var title =$(this).attr("data-dialog-title");

        $(".NoteDialog").live("click", function (e) {

            e.preventDefault();
            if ($(this).attr("data-dialog-title") != "Find Task") 
            {
                if ($(this).attr("data-dialog-id") == "deleteDialog") {
                    $("<div id='formModal'><img src='../../Content/images/ajax-loader.gif' /></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        width: 300,
                        position: [600, 50],
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove() },
                        modal: true,
                        buttons: {
                            "Delete": function () {
                                $("#form0").submit();
                                $(this).dialog("close");
                            },
                            Cancel: function () {
                                $(this).dialog("close");
                            }
                        }
                    })
                .load(this.href);
                }
                else 
                {
                    $("<div id='formModal'><img src='../../Content/images/ajax-loader.gif' /></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        width: 800,
                        position: [300, 50],
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove() },
                        modal: true,
                        buttons: {
                            "Save": function () {
                                alert('Handler for .submit() called.');
                                $("#form0").submit();
                                $(this).dialog("close");
                            },
                            Cancel: function () {
                                $(this).dialog("close");
                            }
                        }
                    })
                .load(this.href);
                }
            }
            else 
            {
                $("<div id='formModal'><img src='../../Content/images/ajax-loader.gif' /></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        width: 860,
                        position: [300, 50],
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove() },
                        modal: true
                })
                .load(this.href);
            }
    });

    $(".close").live("click", function (e) {
        e.preventDefault();
        $(this).closest(".dialog").dialog("close");
    });
});

它用于启动两个模式对话框。发生警报并且关闭对话框也发生,因此脚本正在处理,只是不提交。

部分视图使用 @using (Ajax.BeginForm("AddTime","TimeTracker", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "TimeForm" })) 创建表单

两次产生的表单标签都是:

<form action="/TimeTracker/AddTime?AddID=183336&TypeID=1&_=1328710484230" data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#TimeForm" id="form0" method="post">

是否有不同的提交方式? 我尝试将按钮作为表单本身的一部分,这有效但有不同的问题;取消按钮不会正确关闭模式,导致每次后续启动都会触发多个模式。如果这个问题能够得到解决,我就可以回避这个非发布问题。

更新:我刚刚在使用 XHR 选项卡运行 Firebug 中的场景时发现了一个可能的问题/线索。表单似乎发送到 FindTask 方法,该方法处理第一个模式。它应该发布到 AddTime 方法,这是从主页调用时发生的情况。

为什么 AJAX 不发布到表单标记中指定的操作?

它还发送用于 FindTask 模式的帖子,而不是提交时 AddTime 表单中的表单字段。

I am having a problem with a form submitted in MVC. it is able to be loaded from 2 different points and from one it submits and the second it does not post back.

It is a partial view loaded inside of a JQuery modal dialog. When I trigger the modal from the home page it will submit fine.

There is another modal which can be launched to allow a user to find a task that would then allow for the launch of a second modal to show the form. When this same form is submitted it does not trigger a post to the controller.

Here is the JQuery script:

$(document).ready(function () {
    var title =$(this).attr("data-dialog-title");

        $(".NoteDialog").live("click", function (e) {

            e.preventDefault();
            if ($(this).attr("data-dialog-title") != "Find Task") 
            {
                if ($(this).attr("data-dialog-id") == "deleteDialog") {
                    $("<div id='formModal'><img src='../../Content/images/ajax-loader.gif' /></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        width: 300,
                        position: [600, 50],
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove() },
                        modal: true,
                        buttons: {
                            "Delete": function () {
                                $("#form0").submit();
                                $(this).dialog("close");
                            },
                            Cancel: function () {
                                $(this).dialog("close");
                            }
                        }
                    })
                .load(this.href);
                }
                else 
                {
                    $("<div id='formModal'><img src='../../Content/images/ajax-loader.gif' /></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        width: 800,
                        position: [300, 50],
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove() },
                        modal: true,
                        buttons: {
                            "Save": function () {
                                alert('Handler for .submit() called.');
                                $("#form0").submit();
                                $(this).dialog("close");
                            },
                            Cancel: function () {
                                $(this).dialog("close");
                            }
                        }
                    })
                .load(this.href);
                }
            }
            else 
            {
                $("<div id='formModal'><img src='../../Content/images/ajax-loader.gif' /></div>")
                    .addClass("dialog")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        width: 860,
                        position: [300, 50],
                        title: $(this).attr("data-dialog-title"),
                        close: function () { $(this).remove() },
                        modal: true
                })
                .load(this.href);
            }
    });

    $(".close").live("click", function (e) {
        e.preventDefault();
        $(this).closest(".dialog").dialog("close");
    });
});

It is used for launching both modal dialogs. The Alert occurs and the close dialog occurs also so the script is processing, just not submitting.

The partial view creates the form using @using (Ajax.BeginForm("AddTime","TimeTracker", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "TimeForm" }))

The form tag produced both times is:

<form action="/TimeTracker/AddTime?AddID=183336&TypeID=1&_=1328710484230" data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#TimeForm" id="form0" method="post">

Is there a different way to submit?
I tried having the buttons as part of the form itself which worked but had a different problem; the cancel button wouldn't close the modal correctly, causing multiple modals to be triggered each subsequent launch. If that was able to be remedied, I could sidestep this non-posting issue.

Update: I just found a possible issue/clue when running through the scenario in Firebug with the XHR tab. It appears that the form posts to the FindTask method which is what handles the first modal. It should be posting to the AddTime method which is what occurs when called from the home page.

Why does the AJAX not post to the action specified in the form tag?

It is also sending the post used for the FindTask modal and not the form fields from the AddTime form when submitted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戴着白色围巾的女孩 2025-01-10 21:28:22

问题是 MVC 为两个对话框表单分配了相同的表单 id。它会提交它发现的第一个错误的。

一旦我为表单分配了特定的 ID,我就可以指示要提交哪个表单。

The issue turned out that MVC was assigning the same form id to both dialog forms. It would submit the first it found which was the wrong one.

Once I assigned a specific ID for the form I was able to direct which form to submit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文