Multidimensional Array to MVC Controller

后端 未结 1 406
小蘑菇
小蘑菇 2021-02-08 23:01

I have the following controller method

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


        
相关标签:
1条回答
  • 2021-02-08 23:05

    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);
        }
    });
    
    0 讨论(0)
提交回复
热议问题