所以我有這個類,允許如果色彩被選擇或者不我檢查:在列表中設置對象的布爾值設置爲true
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是一個自定義助手,允許我顯示圖像。
但是,儘管盒子被檢查與否,但控制器中的值始終爲假,那麼我該怎麼做才能糾正?
是否有代碼複選框更改字典的值? – Alexander
嗯,嗯,我認爲渲染的視圖會照顧到,不是嗎?這不是「Html.CheckBoxFor」助手的目的嗎? – hsim
您已將'mBlackColor'' mBlueColor'的值專門分配給'false'。我沒有看到任何代碼沒有設置爲'false' –