Asp.net mvc Ajax post-to-action方法的帮助
我是MVC的新手,需要一些帮助 在我看来,我写了一篇ajax文章,如下所示Asp.net mvc Ajax post-to-action方法的帮助,asp.net-mvc,ajax,post,response,actionlink,Asp.net Mvc,Ajax,Post,Response,Actionlink,我是MVC的新手,需要一些帮助 在我看来,我写了一篇ajax文章,如下所示 function PostCheckedPdf(e) { var values = new Array(); $('input:checked').each(function () { values.push(this.value); }); $.post("/UnregisteredUserPreview/DownloadPdfInvoice"
function PostCheckedPdf(e) {
var values = new Array();
$('input:checked').each(function () { values.push(this.value); });
$.post("/UnregisteredUserPreview/DownloadPdfInvoice",
{ checkedList: values });
}
这将发布在第三方网格组件(Telerik)中选中的任何复选框的值。Action方法接收数组fine并循环遍历每个值,从而生成一个pdf报告,并将报告放入附加到响应的ZipStream中。循环结束后,zipstream关闭,我返回View()
当通过$.post调用操作时,它将通过操作方法运行,但在浏览器中不会发生任何操作
如果我通过操作链接调用该操作(使用两个硬编码值,而不是传递复选框值),则会下载包含所有PDF的zip文件
我做错了什么,或者如何使用ActionLink发布检查值
提前谢谢
托比。不同之处在于,您正在发出一个
标记,该标记正在执行GET
操作。浏览器解释响应的内容并打开PDF
jQuery方法正在执行一个POST
,但对响应不做任何处理,因此会在后台悄悄地将其丢弃
您需要实际处理返回的内容,比如将其写到另一个窗口
var w = window.open('', '', 'width=800,height=600,resizeable,scrollbars');
$.post("/UnregisteredUserPreview/DownloadPdfInvoice",
{ checkedList: values },
function(content){
w.document.write(content);
w.document.close(); // needed for chrome and safari
});
您正在那里对服务器进行Ajax调用,客户端代码应该会收到返回的结果,而您似乎没有这样做。它应该如下所示:
$.ajax({
type: 'POST'
url: '/UnregisteredUserPreview/DownloadPdfInvoice',
data: { checkedList: values },
success: function (r) {
alert(r.result);
}
});
public ActionResult DownloadPdfInvoice() {
//do you stuff here
return Json(new { result = "url_of_your_created_pdf_might_be_the_return_result_here"});
}
并假设您的控制器如下所示:
$.ajax({
type: 'POST'
url: '/UnregisteredUserPreview/DownloadPdfInvoice',
data: { checkedList: values },
success: function (r) {
alert(r.result);
}
});
public ActionResult DownloadPdfInvoice() {
//do you stuff here
return Json(new { result = "url_of_your_created_pdf_might_be_the_return_result_here"});
}
注意
如果您使用锚定标记发布数据,最好
阻止此标记的默认操作,使其不会执行任何操作
除了你要它做的事之外,还有别的。您可以通过添加
以下代码位于单击事件函数末尾:
看看下面的博客文章。它可能会开阔你的思路: