多维数组到MVC控制器

多维数组到MVC控制器

本文介绍了多维数组到MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器方法

    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.

到目前为止,当我使用json数组从jquery进行发布时,我未能获得data参数中除null以外的任何内容.有谁知道填充data参数所需的json正确格式.我正在使用Jquery 1.7.2.

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.

这是我的jquery

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

推荐答案

使用锯齿数组而不是多维数组可能会更好.我获得了更多的成功,使锯齿状的阵列工作.另外爆炸药是正确的,dataType是响应的类型.通过在数据上使用JSON.stringify并指定application\json的contentType,我可以使用json请求来使其工作:

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:

控制器:

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

这篇关于多维数组到MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:54