2011-01-26 14 views

risposta

36

No.

Invece, è possibile scorrere tutte le chiavi, e verificare se essi iniziano con checkbox.

Ad esempio:

foreach(string key in Request.Form) { 
    if (!key.StartsWith("checkbox")) continue; 
    ... 
} 

I NameValueCollection enumerator restituisce i tasti come stringhe.

+0

Perché è stato downvoted? – SLaks

+0

Non da me ... questo era perfetto! Grazie! – MrM

+0

@user: prego. Dovresti accettare questa risposta. – SLaks

4

O qualcosa del genere

var checkBoxes = Request.Form.Keys.Where(rs=>rs.StartsWith("dummy")); 
foreach(string key in checkBoxes){ 
// Your code 
} 
+2

È necessario un 'Cast ()' – SLaks

1

Dovreste essere in grado di fare questo con LINQ (questo dovrebbe funzionare, non hanno testato): che sto assumendo anche che l'ID delle caselle di controllo è "checkbox [ID]". A proposito, l'ID duplicato nei campi del modulo è cattivo male.

var checkboxes = (from key in Request.Form.AllKeys where key = "checkbox" + UniqueIDNum) 
foreach(string key in checkboxes) 
{ 
    //do stuff 
} 
+0

Questo è inutilmente lento. 'AllKeys' copia una matrice. – SLaks

1
 [AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Index(FormCollection fc) 
     { 
      string a = fc["hdn_1"].ToString(); // if you know for sure that it will have a value. 
      string lbl_1 = null; 

      foreach (var key in fc.AllKeys) 
      { 
       if (key.Contains("txt_1")) 
       { 
        lbl_1 = fc["txt_1"].ToString(); 
       } 
      } 
     } 

Spero che questo aiuti.

Problemi correlati