C# asp.net mvc @Html.CheckBoxFor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16688170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
asp.net mvc @Html.CheckBoxFor
提问by Heidel
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?
怎么了?
采纳答案by mattytommo
CheckBoxFortakes a bool, you're passing a List<CheckBoxes>to it. You'd need to do:
CheckBoxFor需要 a bool,你正在传递 aList<CheckBoxes>给它。你需要做:
@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 HiddenForfor the Textproperty too, otherwise you'd lose that when you posted the form, so you wouldn't know which items you'd checked.
请注意,我也HiddenFor为该Text属性添加了一个,否则您在发布表单时会丢失它,因此您将不知道您检查了哪些项目。
Edit, as shown in your comments, your EmploymentTypelist is nullwhen the view is served. You'll need to populate that too, by doing this in your action method:
编辑,如您的评论所示,您的EmploymentType列表是null在提供视图时。您还需要通过在您的操作方法中执行此操作来填充它:
public ActionResult YourActionMethod()
{
CareerForm model = new CareerForm();
model.EmploymentType = new List<CheckBox>
{
new CheckBox { Text = "Fulltime" },
new CheckBox { Text = "Partly" },
new CheckBox { Text = "Contract" }
};
return View(model);
}
回答by mattytommo
Use this code:
使用此代码:
@for (int i = 0; i < Model.EmploymentType.Count; i++)
{
@Html.HiddenFor(m => m.EmploymentType[i].Text)
@Html.CheckBoxFor(m => m.EmploymentType[i].Checked, new { id = "YourId" })
}
回答by Oliver
Html.CheckBoxForexpects a Func<TModel, bool>as the first parameter. Therefore your lambda must return a bool, you are currently returning an instance of List<Checkboxes>:
Html.CheckBoxFor期望 aFunc<TModel, bool>作为第一个参数。因此,您的 lambda 必须返回 a bool,您当前正在返回 的实例List<Checkboxes>:
model => model.EmploymentType
You need to iterate over the List<Checkboxes>to output each checkbox:
您需要遍历List<Checkboxes>以输出每个复选框:
@for (int i = 0; i < Model.EmploymentType.Count; i++)
{
@Html.HiddenFor(m => m.EmploymentType[i].Text)
@Html.CheckBoxFor(m => m.EmploymentType[i].Checked,
new { id = string.Format("employmentType_{0}", i) })
}
回答by Kamil B?dkowski
If only one checkbox should be checked in the same time use RadioButtonFor instead:
如果只应同时选中一个复选框,请改用 RadioButtonFor:
@Html.RadioButtonFor(model => model.Type,1, new { @checked = "checked" }) fultime
@Html.RadioButtonFor(model => model.Type,2) party
@Html.RadioButtonFor(model => model.Type,3) next option...
If one more one could be checked in the same time use excellent extension: CheckBoxListFor:
如果可以同时检查多一个,请使用优秀的扩展:CheckBoxListFor:
Hope,it will help
希望,它会有所帮助

