8

I have the following controller method

    public ActionResult Export(string [,] data, string workbookName)
    {
        ExcelWorkbook workbook = new ExcelWorkbook();
        workbook.AddRows(data);

        return new FileStreamResult(workbook.SaveSheet(), "application/vnd.ms-excel")
        {
            FileDownloadName = workbookName
        };
    }

Which takes a two dimensional array and outputs to a worksheet.

I have failed so far to get anything other than null in the data parameter when posting from jquery with a json array. Does anyone know the correct format of the json needed to populate the data parameter. I am on Jquery 1.7.2.

Here is my jquery

    var arguments = {};

    arguments.data = [["1"], ["2"], ["3"]];
    arguments.workbookName = "test";

    //Populate arrayOfValues 
    $.ajax({
        type: "POST",
        url: '/Excel/Export',
        datatype: "json",
        traditional: true,
        data: arguments,
        success: function (data) {
            alert(data);
        }
    });
2
  • 1
    datatype (I think it should be dataType) is the type of the response, not the request. I don't think this affects you, but the arguments variable looks fine to me. Are you sure you don't mean for arguments.data to be ["1", "2", "3"]? What is the server side language? Commented Jan 14, 2013 at 0:59
  • The server side language is C# Commented Jan 14, 2013 at 9:48

1 Answer 1

9

You may be better off using a jagged array instead of a multi-dimensional array. I have had more success getting a jagged array working. Also Explosion Pills is right, the dataType is the type of the response. I was able to get this working using a json request by using JSON.stringify on the data and specifying the contentType of application\json:

Controller:

    public ActionResult Test(string[][] fields, string workbookName)
    {
        var cr = new JsonResult();
        cr.Data = fields;
        return cr;
    }

JS:

var arguments = {};

arguments.fields = new Array(3);
arguments.fields[0] = new Array(3);
arguments.fields[1] = new Array(3);
arguments.fields[2] = new Array(3);

arguments.fields[0][0] = "hello";

arguments.workbookName = "test";

//Populate arrayOfValues 
$.ajax({
    type: "POST",
    url: '/Home/Test',
    dataType: "json",
    contentType: 'application/json',
    traditional: true,
    data: JSON.stringify(arguments),
    success: function (data) {
        alert(data);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot John, this worked a treat. I changed it to the jagged array instead. Si

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.