2011-01-07 9 views
7

Ciao ho bisogno di passare il mio Request.Form come parametro, ma prima devo aggiungere alcune coppie chiave/valore ad esso. Ottengo l'eccezione che la raccolta è in sola lettura.Serializzazione Request.Form a un dizionario o qualcosa del genere

ho provato:

System.Collections.Specialized.NameValueCollection myform = Request.Form; 

e ottengo lo stesso errore.

e ho provato:

foreach(KeyValuePair<string, string> pair in Request.Form) 
{ 
    Response.Write(Convert.ToString(pair.Key) + " - " + Convert.ToString(pair.Value) + "<br />"); 
} 

per testare se posso passare uno per uno per un altro dizionario, ma ottengo:

System.InvalidCastException: specificata cast non è valido.

qualche aiuto, qualcuno? Grazie

risposta

15

Non è necessario trasmettere un numero string a string. NameValueCollection è costruito attorno a chiavi stringa e valori stringa. Come su un metodo di estensione rapida:

public static IDictionary<string, string> ToDictionary(this NameValueCollection col) 
{ 
    var dict = new Dictionary<string, string>(); 

    foreach (var key in col.Keys) 
    { 
    dict.Add(key, col[key]); 
    } 

    return dict; 
} 

In questo modo si può facilmente andare:

var dict = Request.Form.ToDictionary(); 
dict.Add("key", "value"); 
+0

Grazie Matthew, ci proverò domani. Sono andato con i campi nascosti per il momento. –

+0

matthew - bello (e +1). essendo uno scot, non ho potuto resistere alla mia pugnalata a questo sotto usando un po 'LINQ :). felice anno nuovo - parf ... :-) (btw, avendo una piccola lettura del tuo blog proprio ora, mi piace gli articoli di regula e javascript linq) btw, potresti voler rivedere questa implementazione di LINQ javascript http: //jslinq.codeplex .com/ –

1

Andre,

come circa:

System.Collections.Specialized.NameValueCollection myform = Request.Form; 

IDictionary<string, string> myDictionary = 
    myform.Cast<string>() 
      .Select(s => new { Key = s, Value = myform[s] }) 
      .ToDictionary(p => p.Key, p => p.Value); 

utilizza LINQ per tenere tutto su una "linea". questo potrebbe essere exrapolated ad un metodo di estensione di:

public static IDictionary<string, string> ToDictionary(this NameValueCollection col) 
{ 
    IDictionary<string, string> myDictionary = new Dictionary<string, string>(); 
    if (col != null) 
    { 
     myDictionary = 
      col.Cast<string>() 
       .Select(s => new { Key = s, Value = col[s] }) 
       .ToDictionary(p => p.Key, p => p.Value); 
    } 
    return myDictionary; 
} 

speranza che questo aiuti ..

+0

mantieni la semplicità, eeh? :) – jgauffin

+0

hee hee, aye - :). a volte lo esagero con linq. infatti, ho un dilemma perché mi è stata offerta una nuova opportunità che deve essere fornita nello stack LAMP e mi sto chiedendo come farò a far fronte agli oggetti php5 senza linq :) (così ho visto questo http : //phplinq.codeplex.com/) –

3
public static IEnumerable<Tuple<string, string>> ToEnumerable(this NameValueCollection collection) 
{ 
    return collection 
     //.Keys 
     .Cast<string>() 
     .Select(key => new Tuple<string, string>(key, collection[key])); 
} 

o

public static Dictionary<string, string> ToDictionary(this NameValueCollection collection) 
{ 
    return collection 
     //.Keys 
     .Cast<string>() 
     .ToDictionary(key => key, key => collection[key])); 
} 
9

Se il vostro già utilizzando MVC allora si può farlo con 0 linee di codice.

using System.Web.Mvc; 

var dictionary = new Dictionary<string, object>(); 
Request.Form.CopyTo(dictionary); 
Problemi correlati