I need to display a few checkbox on a form and the user can check as many as hey want.
So I store the checkbox options on Database. (required)
Model
public class Options
{
public int OptionsId {get; set;}
public string Option {get; set;}
}
On the viewModel,
IEnumerable<Options> listCheckBoxOptions {get; set;}// store list of options from database
Dictionary<string,bool> checkboxs {get; set;} // store if is check or not
So on the view i want to store the check box value (true/false) in this checkboxs dictionary.
@foreach (var x in Model.listCheckBoxOptions)
{
@Html.CheckBoxFor(m => m.checkboxs[x.Option])
@m.Option <br />
}
So when i submit the form… the checkboxs is null when gets to the controller.
Any idea why?
Your checkboxes will be given names like this
checkboxs[key here]and ID’s like thischeckboxs_key_here_. MVC doesn’t know how to bind these back.Have a look at this thread that I answered just days ago: Generating an MVC3 RadioButton list in a loop statement
It’s the same thing, just using RadioButtons instead of CheckBoxes.