所以我有这个类可以让我检查是否选择了颜色:
public class Colors
{
public bool mIsSelected { get; set; }
public string mName { get; set; }
}
在将列表发送到视图之前,我使用以下值填充它:
public List<Colors> mListColors = new List<Colors>();
mListColors.Add(new Colors{ mIsSelected = false, mName = "black"});
mListColors.Add(new Colors { mIsSelected = false, mName = "blue" });
mListColors.Add(new Colors { mIsSelected = false, mName = "white" });
mListColors.Add(new Colors { mIsSelected = false, mName = "red" });
mListColors.Add(new Colors { mIsSelected = false, mName = "green" });
在我看来,我将它们渲染成这样:
@for (int i = 0; i < Model.mListColors.Count; i++)
{
var item = Model.mListColors[i];
switch (item.mName)
{
case "black":
@Html.Image("~\\Images\\Functional\\blackColor.jpeg", item.mName.ToUpper(), null)
@Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
@Html.HiddenFor(_item => _item.mListColors[i].mName)
break;
case "blue":
@Html.Image("~\\Images\\Functional\\blueColor.jpeg", item.mName.ToUpper(), null)
@Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
@Html.HiddenFor(_item => _item.mListColors[i].mName)
break;
case "red":
@Html.Image("~\\Images\\Functional\\redColor.jpeg", item.mName.ToUpper(), null)
@Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
@Html.HiddenFor(_item => _item.mListColors[i].mName)
break;
case "green":
@Html.Image("~\\Images\\Functional\\greenColor.jpeg", item.mName.ToUpper(), null)
@Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
@Html.HiddenFor(_item => _item.mListColors[i].mName)
break;
case "white":
@Html.Image("~\\Images\\Functional\\whiteColor.jpeg", item.mName.ToUpper(), null)
@Html.CheckBoxFor(_item => Model.mListColors[i].mIsSelected, item.mIsSelected)
@Html.HiddenFor(_item => _item.mListColors[i].mName)
break;
}
}
这样我可能有一个复选框并将值设置为 true 或 false 取决于是否选中了复选框。HtmlImage 是一个自定义助手,允许我显示图像。
但是无论是否选中该框,控制器中的值始终为假,那么我该怎么做才能更正呢?