模型
public class CustomPInfo
{
public List<PSelect> PSelectList { get; set; }
public List<ExcludeItem> ExcludeItemList { get; set; }
public List<customP> CustomPList { get; set; }
}
public class PSelect
{
public string BClass { get; set; }
public int BClassId { get; set; }
public bool Lvl1 { get; set; }
public bool Lvl2 { get; set; }
public bool Lvl3 { get; set; }
public bool Lvl4 { get; set; }
}
public class ExcludeItem
{
public string Item{ get; set;}
public Nullable<int> BClassId { get; set; }
public bool Exclude { get; set; }
}
public partial class customP
{
public int id { get; set; }
public int Lvl {get; set;}
public Nullable<bool> item1 { get; set; }
public Nullable<bool> item2 { get; set; }
public Nullable<bool> item3 { get; set; }
...
public Nullable<bool> item10 { get; set; }
}
控制器
public ActionResult CustomPanel()
{
CustomPInfo customPInfo = new CustomPInfo();
customPanelInfo.PSelectList = new List<PSelect>();
var pSelectList = from p in db.BClass select new
PSelect { BClass = p.BClass, BClassId = p.id };
foreach (var item in pSelectList)
{
customPInfo.PSelectList.Add(item);
}
customPInfo.ExcludeItemList = new List<ExcludeItem>();
var excludeItemList = from p in db.Item.OrderBy(p => p.BClass_id) select new ExcludeItem { Item = p.Name, BClassId = p.BClass_id };
foreach (var item in excludeItemList)
{
customPInfo.ExcludeItemList.Add(item);
}
customPInfo.CustomPList = new List<customP>();
customP customP = new customP();
var i = 1;
do
{
customP.lvl = i;
customPInfo.CustomPList.Add(customP);
++i;
} while (i <= 4);
return View(customPInfo);
}
[HttpPost]
public ActionResult CustomP(CustomPInfo customPInfo)
{
foreach (var X in customPInfo.ExcludeItemList)
{
if (X.Exclude == false)
{
foreach (var item in customPInfo.PSelectList)
{
if (X.BClassId == item.BClassId)
{
foreach (var CustomP in customPInfo.CustomPList)
{
if (CustomP.custom_panel_id == 1 && item.Lvl1 == true)
{
--How can I set the parameter value of CustomP from a variable?
CustomP. item.item = true;
}
if (CustomP.custom_panel_id == 2 && item.Lvl2 == true)
{
CustomP. item.item = true;
}
if (CustomP.custom_panel_id == 3 && item.Lvl3 == true)
{
CustomP. item.item = true;
}
if (CustomP.custom_panel_id == 4 && item.Lvl4 == true)
{
CustomP. item.item = true;
}
}
}
}
}
}
return View();
}
查看
@{oldBClassId = 0}
@foreach (var X in Model.ExcludeItemList)
{
foreach (var item in Model.PSelectList)
{
if (X.BClassId == item.BClassId)
{
<tr>
@if (oldBClassId != item.BClassId)
{
<td style="border-top: 1px solid #cdd0d4" align="center">
@Html.CheckBoxFor(modelItem => item.Lvl1, new { id = "CBLVL1" })
</td>
<td style="border-top: 1px solid #cdd0d4" align="center">
@Html.CheckBoxFor(modelItem => item.Lvl2, new { id = "CBLVL2" })
</td>
<td style="border-top: 1px solid #cdd0d4" align="center">
@Html.CheckBoxFor(modelItem => item.Lvl3, new { id = "CBLVL3" })
</td>
<td style="border-top: 1px solid #cdd0d4" align="center">
@Html.CheckBoxFor(modelItem => item.Lvl4, new { id = "CBLVL4" })
</td>
<td style="border-top: 1px solid #cdd0d4">
@Html.DisplayFor(modelItem => item.BClass)
@Html.HiddenFor(modelItem => item.BClassId)
</td>
<td style="border-top: 1px solid #cdd0d4">
@Html.DisplayFor(modelItem => X.item)
@Html.HiddenFor(modelItem => X.ClassId)
</td>
<td style="border-top: 1px solid #cdd0d4" align="center">
@Html.CheckBoxFor(modelItem => X.Exclude)
</td>
{
oldBClassId = item.BClassId;
}
}
else
{
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
@Html.DisplayFor(modelItem => X.item)
@Html.HiddenFor(modelItem => X.BClassId)
</td>
<td align="center">
@Html.CheckBoxFor(modelItem => X.Exclude)
</td>
}
</tr>
}
}
“视图”是一个复选框表,允许某人选择一组项目并排除该组中的单个项目。
在控制器中是否有办法遍历项目列表并能够更新模型属性?这将允许从组中选定的复选框设置4个customP中的单个项目,并排除所有4个customP中的单个项目
答案 0 :(得分:0)
由于str变量是一个字符串,你应该使用像这个对象[string]这样的字符串来访问一个对象的属性。
foreach (var str in itemList)
{
model[str] = true;
}