0

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 1
    What are you trying to do here? - your code suggests a major problem with your design. –  Jan 27 '18 at 05:27
  • I was trying to keep it simple in my first post. I updated the code to give a bigger picture of what I am tryin gto do. – JohnnyBaltimore Jan 27 '18 at 19:54
  • Its still not clear what your question is, but your view code can never work and wont bind to anythong - you cannot use a `foreach` loop to generate form controls for a collection. Refer [Post an HTML Table to ADO.NET DataTable](https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943) –  Jan 27 '18 at 21:55

1 Answers1

0

As str variable is a string , you should access properties of an object with string like this object[string].

foreach (var str in itemList)
{
     model[str] = true;
}
Sai Karthik
  • 154
  • 4