I have a checkbox, but the form is being submitted the value ticked are not being submited…
Html:
@foreach (var radiobutton in Model.InterestedIn)
{
<span > @Html.CheckBox("selected", radiobutton)
<label>@radiobutton</label></span>
<br />
}
Model:
[Display(Name = "Would you be interested in receiving *")]
public IList<string> InterestedIn { get; set; }
Controller:
IList<string> lists = new List<string>();
lists.Insert(0, "Latest News");
lists.Insert(1, "Special Offers");
lists.Insert(1, "New Products");
model.InterestedIn = lists;
PostMethod:
[HttpPost]
public ActionResult Index(Competition model)
{
if (ModelState.IsValid)
{
I don’t that your code will compile at all. The
CheckBoxhelper expects a boolean as second argument whereas you are passing it a string.Try like this:
This assumes that you have the following view model:
and the following controller:
If you want to use the
CheckBoxor even better theCheckBoxForhelper you will have to adapt your view model so that it no longer has anIList<string>property but anIList<CheckBoxItemViewModel>property whereCheckBoxItemViewModelis another view model that will contain the label and a boolean property indicating whether this value has been selected or not.