有关 Ajax post to action 方法的帮助

发布于 2024-12-07 11:38:45 字数 668 浏览 5 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(2

看春风乍起 2024-12-14 11:38:45

区别在于您的 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
      });

The difference is that your ActionLink is emitting an <a> tag, which is performing a GET 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.

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
      });
情域 2024-12-14 11:38:45

您正在那里对服务器进行 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"});
}

注意

如果您使用 anchor 标签发布数据,最好
阻止此标签的默认操作,使其不会执行任何操作
除了你告诉它要做的事情之外的其他事情。您可以通过添加
click 事件函数末尾添加以下代码:

$("#myLink").click(function(e) { 

    //do the logic here
    //ajax call, etc.

    e.preventDefault();
});

另请参阅下面的博客文章。它可能会拓宽您的想法:

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 :

$.ajax({

  type: 'POST'
  url: '/UnregisteredUserPreview/DownloadPdfInvoice',
  data: { checkedList: values },
  success: function (r) {

      alert(r.result);
  }

});

And assume that your controller is like below :

public ActionResult DownloadPdfInvoice() { 

    //do you stuff here
    return Json(new { result = "url_of_your_created_pdf_might_be_the_return_result_here"});
}

NOTE

If you are posting your data with anchor tag, it is better to
prevent the default action of this tag so that it won't do anything
else but the thing you're telling it to do. You can do that by adding the
following code at the end of your click event function :

$("#myLink").click(function(e) { 

    //do the logic here
    //ajax call, etc.

    e.preventDefault();
});

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

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