Model
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; }
}
Controller
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();
}
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>
}
}
The View is a table of checkboxes that allow someone to select a group of items and exclude a single item of that group.
In the controller is there a way to loop through a list of the item's and be able to update the model properties? This would allow the setting of individual items in 4 customPs from a selected checkbox in the group and exclude individual items in all of the 4 customPs