替代 JQuery form.submit() 进行 ajax post
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
这将发布您的表单并在回调中返回部分视图。 `
Try:
This will post your form and return the partial view in the callback. `
您是否尝试过 $.post() 或 $.ajax()?
have you tried $.post() or $.ajax()?
恕我直言,MS MVC AJAX 已损坏。我发现使用 jquery 提交表单要好得多,如下所示:
确保您使用部分来呈现数据,并使用 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:
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.
如果您想坚持使用 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.