有关 Ajax post to action 方法的帮助
我是 MVC 新手,需要一点帮助。
在我看来,我发布了一个ajax帖子,如下所示。
function PostCheckedPdf(e) {
var values = new Array();
$('input:checked').each(function () { values.push(this.value); });
$.post("/UnregisteredUserPreview/DownloadPdfInvoice",
{ checkedList: values });
}
此帖子发布在第三方网格组件 (Telerik) 内选中的任何复选框的值。 Action 方法接收数组并循环遍历每个值,呈现 pdf 报告并将报告放入附加到 Response 的 ZipStream 中。循环结束后,zipstream 关闭,我返回 View();
当通过 $.post 调用 Action 时,它会运行操作方法,但浏览器中没有任何反应。
如果我通过操作链接调用操作(使用几个硬编码值而不是传递复选框值),则会下载包含所有 pdf 的 zip 文件。
我做错了什么或者如何使用 ActionLink 发布检查的值?
提前致谢!
托比.
I am a new to MVC an need a little help.
In my view I make an ajax post as below.
function PostCheckedPdf(e) {
var values = new Array();
$('input:checked').each(function () { values.push(this.value); });
$.post("/UnregisteredUserPreview/DownloadPdfInvoice",
{ checkedList: values });
}
This post the values of any checkboxes that are checked inside a third party Grid component (Telerik). The Action method receives the array fine and loops through each value rendering a pdf report and putting the report into a ZipStream which is attached to the Response. After the loop the zipstream is closed and I return View();
When the Action is invoked through the $.post it runs through the action method but nothing happens in the browser.
If I call the Action through an action link (with a couple of hard coded value instead of passing the checked boxes values) the zip file with all the pdfs is downloaded.
What am I doing wrong or how can I post the checked values with an ActionLink?
Thanks in Advance!
Toby.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
区别在于您的 ActionLink 正在发出
标记,该标记正在执行
GET
操作。浏览器解释响应的内容并打开 PDF。您的 jQuery 方法正在执行
POST
,但对响应不执行任何操作,因此默默地将其丢弃在后台。您需要对返回内容进行实际操作,例如将其写入另一个窗口。
The difference is that your ActionLink is emitting an
<a>
tag, which is performing aGET
operation. The browser interprets the contents of the response and opens the PDF.Your jQuery method is performing a
POST
, but does nothing with the response, and thus silently throws it away in the background.You need to actually do something with the return contents, like write it out to another window.
您正在那里对服务器进行 Ajax 调用,客户端代码应该收到返回的结果,但这似乎您没有在那里执行。它应该如下所示:
并假设您的控制器如下所示:
另请参阅下面的博客文章。它可能会拓宽您的想法:
You are making an Ajax call to the server there and client side code should receive the returned result which seems that you are not doing there. It should be something like below :
And assume that your controller is like below :
Have a look at the below blog post as well. It might widen your thoughts :
http://www.tugberkugurlu.com/archive/working-with-jquery-ajax-api-on-asp-net-mvc-3-0-power-of-json-jquery-and-asp-net-mvc-partial-views