asp.net mvc @Html.CheckBoxFor

CheckBoxFor takes a bool, you're passing a List<CheckBoxes> to it. You'd need to do:

@for(int i =0; i <Model.EmploymentType.Count; i++){@Html.CheckBoxFor(m => m.EmploymentType[i].Checked,new{ id ="employmentType_"+ i })@Html.HiddenFor(m => m.EmploymentType[i].Text)@Html.DisplayFor(m => m.EmploymentType[i].Text)}

Notice I've added a HiddenFor for the Text 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 is null when the view is served. You'll need to populate that too, by doing this in your action method:

publicActionResultYourActionMethod(){CareerForm model =newCareerForm();

    model.EmploymentType=newList<CheckBox>{newCheckbox{Text="Fulltime"},newCheckbox{Text="Partly"},newCheckbox{Text="Contract"}};returnView(model);}
原文地址:https://www.cnblogs.com/happy-Chen/p/3688561.html