2012-01-15 18 views
5

Ho un array in javascript e ho bisogno di scaricarlo nel mio C# webMethod. qual è il modo migliore per farlo?Passa un array da javascript a C#

il mio codice C# è come questo:

[WebMethod] 
public static void SaveView(string[] myArray, string[] filter) 
{ 
} 

EDIT--

miei dati JSON è simile al seguente:

{"myArray":[{"name":"Title","index":"Title","hidden":false,"id":"1","sortable":true,"searchoptions":{"sopt":["cn","eq","bw","ew"]},"width":419,"title":true,"widthOrg":150,"resizable":true,"label":"Title","search":true,"stype":"text"},{"name":"Author","index":"Author","hidden":false,"id":"3","sortable":true,"searchoptions":{"sopt":["cn","eq","bw","ew"]},"width":419,"title":true,"widthOrg":150,"resizable":true,"label":"Author","search":true,"stype":"text"}]} 

Ma io non lavorare ... qualche idea del perché?

Grazie mille.

+1

-1 La tua domanda aggiornato è completamente diverso da quello originale. Come convertire l'array di oggetti nell'array di stringhe '{" name ":" Title ", " index ":" Title ", " hidden ": false, " id ":" 1 ", " sortable ": true, "searchoptions": { "sopt": [ "cn", "eq", "bw", "ew" ] },' –

risposta

16

È possibile inviarlo come stringa JSON. Ecco un esempio utilizzando jQuery:

var array = [ 'foo', 'bar', 'baz' ]; 
$.ajax({ 
    url: '/foo.aspx/SaveView', 
    type: 'POST', 
    contentType: 'application/json', 
    data: JSON.stringify({ myArray: array }), 
    success: function(result) { 

    } 
}); 

Se il metodo restituisce Pagina qualcosa, si dovrebbe utilizzare la proprietà result.d nella callback successo per andare a prendere il risultato della chiamata al metodo pagina.

Se non si utilizza jQuery, sarà necessario tenere conto manualmente delle differenze tra browser nell'invio della richiesta AJAX. Ma per questo al lavoro ci sono 2 cose fondamentali da includere nella richiesta:

  • L'intestazione richiesta Content-Type deve essere impostato su application/json
  • La richiesta payload deve essere JSON, ad esempio: { myArray: [ 'foo', 'bar', 'baz' ] }

UPDATE:

Ora che avete aggiornato la tua domanda sembra che non si è più disposti a inviare una serie di str Ings. Quindi definire un modello che abbinerà la struttura JSON si sta inviando:

public class Model 
{ 
    public string Name { get; set; } 
    public string Index { get; set; } 
    public bool Hidden { get; set; } 
    public int Id { get; set; } 
    public bool Sortable { get; set; } 
    public SearchOption Searchoptions { get; set; } 
    public int Width { get; set; } 
    public bool Title { get; set; } 
    public int WidthOrg { get; set; } 
    public bool Resizable { get; set; } 
    public string Label { get; set; } 
    public bool Search { get; set; } 
    public string Stype { get; set; } 
} 

public class SearchOption 
{ 
    public string[] Sopt { get; set; } 
} 

e poi:

[WebMethod] 
public static void SaveView(Model[] myArray) 
{ 
} 
+0

Grazie, come dovrebbe il mio sguardo WebMethod ? – Ovi

+0

@Ovi, come ti aspetti che io sappia questo? Dipenderà da ciò che vuoi che questo metodo faccia. Ma la firma che hai mostrato è corretta e riceverà l'argomento 'string []': '[WebMethod] public static void SaveView (string [] myArray) {...}'. –

+0

Il tuo metodo web sembra a posto - devi fare con l'array qualunque sia il tuo requisito specifico. Tutto dipende da te e non possiamo davvero aiutarti in questo. :) – FarligOpptreden

2
var xhr = new XMLHttpRequest(); 
xhr.open("POST", "mypage/SaveView"); 
xhr.setRequestHeader("Content-Type", "application/json"); 
xhr.send(JSON.stringify({ myArray: someArray })); 
+0

No, questo non funzionerà. 'myArray = ...' non è valido JSON. Devi stringificare l'intero letterale. Vedi la mia risposta per un esempio. –

+0

no, in questo caso specifico si desidera JSON, altrimenti il ​​PageMethod di ASP.NET che l'OP sta tentando di richiamare non funzionerà. ASP.NET PageMethods si aspettano richieste codificate JSON e inviano risultati codificati JSON. I telemetodi –

+0

@DarinDimitrov non funzionano con "application/x-www-form-urlencoded"? – Raynos