替代 JQuery form.submit() 进行 ajax post

发布于 2024-08-26 23:24:50 字数 1233 浏览 6 评论 0原文

我有一个 mvc2 应用程序,其视图如下

    <% using (Ajax.BeginForm("UserApprove", new { id = Model.id }, new AjaxOptions() { UpdateTargetId = "statusLights", OnSuccess = "HideButtons" }, new { id = "UserApprove" }))
   {%>

<input type="button" value="Approve" onclick="$('#dialogApprove').dialog('open')" />
<div id="dialogApprove" title="Confirm">
    <p>
        Are you sure you want to approve this request?
    </p>
</div>
<% } %>

仅供参考,控制器返回部分视图。

我以前没有 jquery 对话框,只是简单的一个,

<input type="Submit" value="Approve" /> 

以前工作得很好,

我添加了 jquery 对话框,我有类似的东西来初始化对话框。

 $("#dialogApprove").dialog({
        autoOpen: false,
        draggable: true,
        resizable: false,
        buttons: {
            "Cancel": function() {

                $(this).dialog("close")
            },
            "Approve": function() {
                $("#UserApprove").submit();
                $(this).dialog("close");
            }
        }
    });

$("#UserApprove").submit();似乎没有做 ajax 帖子。它仅返回新页面中返回的部分视图中的文本。

我不想使用具有 .ajaxSubmit() 的 jquery 表单插件。有没有其他方法可以强制从 jquery 对话框“批准”按钮发送 ajax 帖子?

I have a mvc2 application with a view like this

    <% using (Ajax.BeginForm("UserApprove", new { id = Model.id }, new AjaxOptions() { UpdateTargetId = "statusLights", OnSuccess = "HideButtons" }, new { id = "UserApprove" }))
   {%>

<input type="button" value="Approve" onclick="$('#dialogApprove').dialog('open')" />
<div id="dialogApprove" title="Confirm">
    <p>
        Are you sure you want to approve this request?
    </p>
</div>
<% } %>

FYI, the controller returns a partial view back.

I used to not have the jquery dialog and just simple a

<input type="Submit" value="Approve" /> 

that used to work fine

I added the jquery dialog and I have something like this to initialize the dialog.

 $("#dialogApprove").dialog({
        autoOpen: false,
        draggable: true,
        resizable: false,
        buttons: {
            "Cancel": function() {

                $(this).dialog("close")
            },
            "Approve": function() {
                $("#UserApprove").submit();
                $(this).dialog("close");
            }
        }
    });

The $("#UserApprove").submit(); does not seem to be doing an ajax post. It comes back with just the text from the partial view returned in a new page.

I dont want to use the jquery form plugin which has .ajaxSubmit(). Is there any other way to force an ajax post from the jquery dialog "approve" button?

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

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

发布评论

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

评论(4

你不是我要的菜∠ 2024-09-02 23:24:50

尝试:

$("#dialogApprove").dialog({
    autoOpen: false,
    draggable: true,
    resizable: false,
    buttons: {
        "Cancel": function() {

            $(this).dialog("close")
        },
        "Approve": function() {
            $.post($("#UserApprove").attr('action'), $("#UserApprove").serialize(), 
                   function(result) {
                    // result will be your partial view
                    $(this).dialog("close");
                   }
            );
        }
    }
});

这将发布您的表单并在回调中返回部分视图。 `

Try:

$("#dialogApprove").dialog({
    autoOpen: false,
    draggable: true,
    resizable: false,
    buttons: {
        "Cancel": function() {

            $(this).dialog("close")
        },
        "Approve": function() {
            $.post($("#UserApprove").attr('action'), $("#UserApprove").serialize(), 
                   function(result) {
                    // result will be your partial view
                    $(this).dialog("close");
                   }
            );
        }
    }
});

This will post your form and return the partial view in the callback. `

夏末 2024-09-02 23:24:50

您是否尝试过 $.post()$.ajax()?

"Approve": function() {
      var form = $('#userapprove');
      $.post(form.attr('action'), form.serialize());
      $(this).dialog("close");
}

have you tried $.post() or $.ajax()?

"Approve": function() {
      var form = $('#userapprove');
      $.post(form.attr('action'), form.serialize());
      $(this).dialog("close");
}
小矜持 2024-09-02 23:24:50

恕我直言,MS MVC AJAX 已损坏。我发现使用 jquery 提交表单要好得多,如下所示:

$.ajax({
                        type: $("#UserApprove").attr("method"),
                        url: $("#UserApprove").attr("action"),
                        data: $("#UserApprove").serialize(),
                        success: function(data, textStatus, XMLHttpRequest) {
                            $("#SomeDiv").html(data); //replace the reports html.

                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {

                            alert("Yikers! The AJAX form post didn't quite go as planned...");
                        }
                    });

确保您使用部分来呈现数据,并使用 Request.IsAjaxRequest() 将数据发送到部分。

请参阅我的帖子此处 以获得更完整的代码。

imho, MS MVC AJAX is broken. I have found that it is far better to use jquery to submit the form like this:

$.ajax({
                        type: $("#UserApprove").attr("method"),
                        url: $("#UserApprove").attr("action"),
                        data: $("#UserApprove").serialize(),
                        success: function(data, textStatus, XMLHttpRequest) {
                            $("#SomeDiv").html(data); //replace the reports html.

                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {

                            alert("Yikers! The AJAX form post didn't quite go as planned...");
                        }
                    });

Make sure that you are using a partial to render the data and that you use Request.IsAjaxRequest() to send the data to the partial.

See my post here for more complete code.

指尖微凉心微凉 2024-09-02 23:24:50

如果您想坚持使用 MVC Ajax 帮助器,您应该向 AjaxOptions 添加一条确认消息。这将使用标准的 javascript 确认对话框来显示您的消息,并且仅在操作得到确认后才继续。如果您想使用 jQuery UI 对话框,那么我建议使用普通表单并使用 .ajax().post() 在“批准”按钮处理程序。本质上,您将编写与辅助方法插入的相同代码以使用 AJAX 执行帖子,然后用返回的 HTML 替换目标内容。

If you want to stick with the MVC Ajax helper, you should add a Confirm message to your AjaxOptions. This will use the standard javascript confirmation dialog to display your message and only proceed if the action is confirmed. If you want to use a jQuery UI dialog, then I'd suggest using a normal form and having the dialog submit using .ajax() or .post() in the "Approve" button handler. Essentially, you'd be writing the same code that the helper method is inserting to do the post using AJAX then replacing the contents of the target with the returned HTML.

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