Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7905659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:33:51+00:00 2026-06-03T10:33:51+00:00

I’m trying to do something that seems like it should be easy but I’m

  • 0

I’m trying to do something that seems like it should be easy but I’m new to MVC and convention-based programming.

I have a jQuery datatable that is getting rows for PDF documents through AJAX. In the fnRowCallback, I added checkboxes so that the user can select multiple documents to be combined for a single download. As checkboxes are checked, the document ID is added to a JavaScript array of numbers and the filenames are added to another array so that, when combined, they can be used for bookmarks in the resulting PDF. Is there any way to send these two variables to the controller action? So far, all I’ve been able to do is JSON.stringify() one of the variables and send it to the controller using a hidden field in a form I put on the view and deserializing it in the controller but I’m goofing it up when I try to add the second variable. There has to be an easier way but I can’t even figure out the convoluted way and all articles I’ve read use AJAX. I can’t use AJAX, though, because you can’t send a binary file back in the response.

JavaScript:

var aiSelectedPDFs = new Array();
var aiSelectedDocumentIDs = new Array();

$('#imgDownload').click(function () {
    $('#selectedPDFs').val(JSON.stringify(aiSelectedPDFs));
    $('#selectedDocumentIDs').val(JSON.stringify(aiSelectedDocumentIDs));
    $('#DownloadSelectedPdfs').submit();
});

View:

<img id="imgDownload" src="@(Url.RootUrl())Content/images/icons/pdf.gif" 
    alt="Download selected documents" title="Download selected documents" />
@using (Html.BeginForm("DownloadSelectedPdfs", "Controller", FormMethod.Post, 
    new { id = "DownloadSelectedPdfs" }))
{
    <input type="hidden" id="selectedPdfs" name="jsonSelectedPdfs"/>
    <input type="hidden" id="selectedDocumentIDs" name="jsonSelectedDocumentIDs"/>
}

Controller:

    [HttpPost]
    public ActionResult DownloadSelectedPdfs(string jsonSelectedDocumentIDs)
    {
        var selectedDocumentIDs = new JavaScriptSerializer().Deserialize<int[]>(
            jsonSelectedDocumentIDs);
        var invoices = new Dictionary<string, byte[]>();

        foreach (int documentID in selectedDocumentIDs)
        {
            invoices.Add(documentID.ToString(), 
                _documentService.GetDocument(documentID));
        }

        return new FileContentResult(PdfMerger.MergeFiles(invoices), 
            "application/pdf");
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T10:33:52+00:00Added an answer on June 3, 2026 at 10:33 am

    Your answer is 90% correct, Kroehre. Thank you for such a quick response. The only issue is that the application cannot figure out what controller action to use so the page fails to load and I get redirected to my friendly error page. The solution to that was pretty simple, though, and I’ll put the code below.

    View (I left out the divs as I felt they sullied up the code with presentation semantics where nothing was to be displayed, though they had no syntactical impact. Just personal preference so not part of the missing 10%. 😉 ):

    <img id="imgDownload" src="@(Url.RootUrl())Content/images/icons/pdf.gif" 
        alt="Download selected documents" title="Download selected documents" />
    @using (Html.BeginForm("DownloadSelectedPdfs", "Controller", FormMethod.Post, 
        new { id = "DownloadSelectedPdfs" })) { }
    

    Script (Created similar multiple hidden inputs but with naming such that they were the same object with properties):

    var aiSelectedPDFs = new Array();
    var aiSelectedDocumentIDs = new Array();
    
    $('#imgDownload').click(function () {
        var form = $('#DownloadSelectedPdfs');
        form.html('');
        for (var i = 0; i < aiSelectedPDFs.length; i++) {
            form.append('<input type="hidden" name="selectedPDFs[' + i + '].RefNumber" 
                value="' + aiSelectedPDFs[i] + '" />');
            form.append('<input type="hidden" name="selectedPDFs[' + i + '].DocumentID" 
                value="' + aiSelectedDocumentIDs[i] + '" />');
        }
        form.submit();
    });
    

    Controller (added new class to handle the multiple, related javascript variables):

    public class PDFViewModel
    {
        public int RefNumber { get; set; }
        public int DocumentID { get; set; }
    }
    
    [HttpPost]
    public ActionResult DownloadSelectedPdfs(List<PDFViewModel> selectedPDFs)
    {
        var pdfs = new Dictionary<string, byte[]>();
    
        foreach (var selectedPDF in selectedPDFs)
        {
            var document = _documentService.GetDocument(selectedPDF.DocumentID);
            var tabName = string.Format("pdf_{0}", selectedPDF.RefNumber);
            pdfs.Add(tabName, document);
        }
    
        return new FileContentResult(PdfMerger.MergeFiles(pdfs), "application/pdf");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.