I have a form which contains a whole bunch of checkboxes and some other types of control too. I need to retrieve the names of each selected checkbox.
What is the bes
Unfortunately that type of information isn't available in the collection. However if you prepend all your checkboxes with something like then you can run a query like
var names = formCollection.AllKeys.Where(c => c.StartsWith("checkbox"));
Since only the checked values will be posted back you don't need to validate that they're checked.
Here's one that grabs only checked values
var names = formCollection.AllKeys.Where(c => c.StartsWith("test") &&
formCollection.GetValue(c) != null &&
formCollection.GetValue(c).AttemptedValue == "1");