title: Is there a way to dynamically set model values in a controller?
body: <p>Model</p>
  
  <pre><code>public class CustomPInfo
  {
      public List&lt;PSelect&gt; PSelectList { get; set; }
      public List&lt;ExcludeItem&gt; ExcludeItemList { get; set; }
      public List&lt;customP&gt; 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&lt;int&gt; BClassId { get; set; }
      public bool Exclude { get; set; }
  }
  
  public partial class customP
  {
      public int id { get; set; }
      public int Lvl {get; set;}
      public Nullable&lt;bool&gt; item1 { get; set; }
      public Nullable&lt;bool&gt; item2 { get; set; }
      public Nullable&lt;bool&gt; item3 { get; set; }
  ...
      public Nullable&lt;bool&gt; item10 { get; set; }        
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public ActionResult CustomPanel()
      {
          CustomPInfo customPInfo = new CustomPInfo();
  
          customPanelInfo.PSelectList = new List&lt;PSelect&gt;();
          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&lt;ExcludeItem&gt;();
  
          var excludeItemList = from p in db.Item.OrderBy(p =&gt; 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&lt;customP&gt;();
          customP customP = new customP();
          var i = 1;
          do
          {
              customP.lvl = i;
              customPInfo.CustomPList.Add(customP);
              ++i;
          } while (i &lt;= 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 &amp;&amp; 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 &amp;&amp; item.Lvl2 == true)
                              {
                                CustomP. item.item  = true;                                    
                              }
                              if (CustomP.custom_panel_id == 3 &amp;&amp; item.Lvl3 == true)
                              {
                                CustomP. item.item  = true;                                    
                              }
                              if (CustomP.custom_panel_id == 4 &amp;&amp; item.Lvl4 == true)
                              {
                                CustomP. item.item  = true;                                    
                              }
                          }
                      }
                  }
              }
  
          }
  
          return View();
      }
  </code></pre>
  
  <p>View</p>
  
  <pre><code>@{oldBClassId = 0}
  @foreach (var X in Model.ExcludeItemList)
  {
   foreach (var item in Model.PSelectList)
   {
      if (X.BClassId == item.BClassId)
      {
        &lt;tr&gt;
        @if (oldBClassId != item.BClassId)
        {
         &lt;td style="border-top: 1px solid #cdd0d4" align="center"&gt;
          @Html.CheckBoxFor(modelItem =&gt; item.Lvl1, new { id = "CBLVL1" })
         &lt;/td&gt;
         &lt;td style="border-top: 1px solid #cdd0d4" align="center"&gt;
         @Html.CheckBoxFor(modelItem =&gt; item.Lvl2, new { id = "CBLVL2" })
         &lt;/td&gt;
         &lt;td style="border-top: 1px solid #cdd0d4" align="center"&gt;
         @Html.CheckBoxFor(modelItem =&gt; item.Lvl3, new { id = "CBLVL3" })
         &lt;/td&gt;
         &lt;td style="border-top: 1px solid #cdd0d4" align="center"&gt;
         @Html.CheckBoxFor(modelItem =&gt; item.Lvl4, new { id = "CBLVL4" })
         &lt;/td&gt;
         &lt;td style="border-top: 1px solid #cdd0d4"&gt;
         @Html.DisplayFor(modelItem =&gt; item.BClass)
         @Html.HiddenFor(modelItem =&gt; item.BClassId)
         &lt;/td&gt;
         &lt;td style="border-top: 1px solid #cdd0d4"&gt;
         @Html.DisplayFor(modelItem =&gt; X.item)
         @Html.HiddenFor(modelItem =&gt; X.ClassId)
         &lt;/td&gt;
         &lt;td style="border-top: 1px solid #cdd0d4" align="center"&gt;
         @Html.CheckBoxFor(modelItem =&gt; X.Exclude)
         &lt;/td&gt;
         {
          oldBClassId = item.BClassId;
         }
         }
         else
         {
         &lt;td&gt;&lt;/td&gt;
         &lt;td&gt;&lt;/td&gt;
         &lt;td&gt;&lt;/td&gt;
         &lt;td&gt;&lt;/td&gt;
         &lt;td&gt;&lt;/td&gt;
         &lt;td&gt;
         @Html.DisplayFor(modelItem =&gt; X.item)
         @Html.HiddenFor(modelItem =&gt; X.BClassId)
         &lt;/td&gt;
         &lt;td align="center"&gt;
         @Html.CheckBoxFor(modelItem =&gt; X.Exclude)
         &lt;/td&gt;
         }
      &lt;/tr&gt;
      }
     }
  </code></pre>
  
  <p>The View is a table of checkboxes that allow someone to select a group of items and exclude a single item of that group.</p>
  
  <p>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</p>