I have a form that contains a whole bunch of checkboxes and some other types of controls. I need to get the names of each selected checkbox.
What is the best way to do this? Can I do this with linq query?
So, in the pseudocode, I want to do something like this:
var names = formCollection .Where(c => c is Checkbox && c.Checked) .Select(c => c.Name);
Refresh MVC seems to present checkboxes different from how the normal form will behave, since a hidden field is also displayed. I found the details here: How to handle checkboxes in ASP.NET MVC forms?
Anywho, it works for me using this 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));
checkbox asp.net-mvc controls webforms formcollection
fearofawhackplanet
source share