2013-01-13 29 views
8

Ho il seguente metodo controlloremultidimensionale Array Controller 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 
     }; 
    } 

che prende un array bidimensionale e uscite a un foglio.

Non sono riuscito finora a ottenere qualcosa di diverso da null nel parametro data durante la pubblicazione da jQuery con un array JSON. Qualcuno sa il formato corretto del JSON necessario per popolare il parametro dei dati. Sono su Jquery 1.7.2.

Ecco il mio 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); 
     } 
    }); 
+1

'datatype' (I pensa che dovrebbe essere 'dataType') è il tipo di * risposta *, non la richiesta. Non penso che questo ti riguardi, ma la variabile 'arguments' mi sta bene. Sei sicuro che non intendi per 'arguments.data' essere' ["1", "2", "3"] '? Qual è la lingua lato server? –

+0

La lingua lato server è C# – Sico

risposta

8

Potrebbe essere meglio utilizzare una matrice irregolare invece di un array multi-dimensionale. Ho avuto più successo nel far funzionare un array frastagliato. Anche Explosion Pills ha ragione, il dataType è il tipo di risposta. Sono stato in grado di ottenere questo lavoro utilizzando una richiesta JSON utilizzando JSON.stringify sui dati e specificando il contentType di application\json:

controllore:

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

Grazie mille John, questo ha funzionato. L'ho cambiato invece nell'array frastagliato. Si – Sico