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 best way to do this? Can I do it with a linq query maybe?
So in pseudocode, I'm looking to do something like this:
var names = formCollection
.Where(c => c is Checkbox && c.Checked)
.Select(c => c.Name);
Update It seems the way MVC submits checkboxes is different from how a normal form would behave, as an hidden field is also rendered. I found the details here: How to handle checkboxes in ASP.NET MVC forms?
Anywho, I've got it working with the help of that thread and the answer from BuildStarted below. The following code did the trick.
var additionalItems = form.AllKeys
.Where(k => form[k].Contains("true") && k.StartsWith("addItem"))
.Select(k => k.Substring(7));