我目前正在研究MVC應用程序並將一些信息保存到數據庫中。從這個信息我保存它保存在自己的列從數據庫獲取值爲複選框的問題
string 1, string 2, string 3
一些複選框值現在我試圖檢索這些值,使複選框在不同的頁面進行檢查。但是,我應該得到兩個複選框,我只有一個。返回的值是正確的,但對於一些奇怪的原因只有一個複選框在視圖中顯示
在我的控制,我有以下代碼
IEnumerable<MyEntity> myEntity = entityRepo.GetAll().Where(a => a.UserId == id);
List<CheckBox> checkBoxList = new List<CheckBox>();
foreach (var items in myEntity)
{
checkBoxList.Add(
new CheckBox
{
Text = items.EightWaste,
Checked = true,
Value = items.EightWaste,
});
}
然後在視圖中我有
@foreach(var items in Model.EightWatseChkBox)
{
@Html.DisplayFor(model => items.Text)
@Html.CheckBoxFor(model => items.Checked)
}
而我的UI輸出看起來像
有人可以告訴我我要去哪裏錯了嗎。
什麼是myEntity.Count()的'價值;'? - 它看起來像你只有一個物品,它的'EightWaste'值是'「缺陷,運輸」' –
並且作爲一個附註,你'foreach'循環不會綁定到你的模型。你需要使用'for'循環或'EditorTemplate'來輸入'CheckBox'類型 –
你檢查了這個問題:http://stackoverflow.com/questions/14730746/getting-checkbox-value-in-asp-net-mvc -4? – JJP