CheckboxList in MVC3 View and get the checked items passed to the controller

Let’s modify your model a little: public class ItemViewModel { public string Id { get; set; } public string Name { get; set; } public bool Checked { get; set; } } then you could have a controller: public class HomeController: Controller { public ActionResult Index() { // This action is used to render the … 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();

tech