I have checkboxes in my form
I added at my model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CorePartners_Site2.Models
{
public class CareerForm
{
//....
public List<CheckBoxes> EmploymentType { get; set; }
}
}
public class CheckBoxes
{
public string Text { get; set; }
public bool Checked { get; set; }
}
and added at my form
@Html.CheckBoxFor(model => model.EmploymentType, new { id = "employmentType_1" })
@Html.CheckBoxFor(model => model.EmploymentType, new { id = "employmentType_2" })
@Html.CheckBoxFor(model => model.EmploymentType, new { id = "employmentType_3" })
but I get the mistake
What's wrong?
If only one checkbox should be checked in the same time use RadioButtonFor instead:
If one more one could be checked in the same time use excellent extension: CheckBoxListFor:
Hope,it will help
Use this code:
Html.CheckBoxFor
expects aFunc<TModel, bool>
as the first parameter. Therefore your lambda must return abool
, you are currently returning an instance ofList<Checkboxes>
:You need to iterate over the
List<Checkboxes>
to output each checkbox:CheckBoxFor
takes abool
, you're passing aList<CheckBoxes>
to it. You'd need to do:Notice I've added a
HiddenFor
for theText
property too, otherwise you'd lose that when you posted the form, so you wouldn't know which items you'd checked.Edit, as shown in your comments, your
EmploymentType
list isnull
when the view is served. You'll need to populate that too, by doing this in your action method: