How to create a CheckBoxListFor extension method in ASP.NET MVC?

Here is a strongly typed HtmlHelper for CheckBoxListFor that handles selected items as an array in your viewdata model. I chose not to wrapper the Html.CheckBox or Html.CheckBoxFor methods as I don’t want the hidden “false” fields in my checkbox lists. Please feel free to improve on this and repost 🙂 //View <%: Html.CheckBoxListFor(model => … Read more

How to get values of selected items in CheckBoxList with foreach in ASP.NET C#?

Note that I prefer the code to be short. List<ListItem> selected = CBLGold.Items.Cast<ListItem>() .Where(li => li.Selected) .ToList(); or with a simple foreach: List<ListItem> selected = new List<ListItem>(); foreach (ListItem item in CBLGold.Items) if (item.Selected) selected.Add(item); If you just want the ListItem.Value: List<string> selectedValues = CBLGold.Items.Cast<ListItem>() .Where(li => li.Selected) .Select(li => li.Value) .ToList();