开发者

Help with Ajax post to action method

开发者 https://www.devze.com 2023-04-08 21:37 出处:网络
I am a new to MVC an need a little help. In my view I make an ajax post as below. function PostCheckedPdf(e) {

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 recei开发者_JAVA百科ves 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.


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
      });


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

0

精彩评论

暂无评论...
验证码 换一张
取 消