title: Pass IEnumerable to controller
body: <p>I am trying to pass an <code>IEnumerable</code> to my controller so that I can loop through each row, make some modifications and add it the database. The problem is that the <code>IEnumerable</code> is <code>Null</code> when it is passed back to the controller.</p>
  <p>I have read that I need to change my list to <code>IList</code> and use For instead of ForEach - which I understand..., however, now my <code>IEnumerable</code> needs to be passed to the View as a <code>IList</code> and I'm not sure how to do this.</p>
  <p>So, I have a couple of questions.</p>
  <ol>
  <li>Is <code>IList</code> the only way to go, can I not pass the model to the controller as an <code>IEnumerable</code>?</li>
  <li>If I have to go with <code>IList</code>, how do I change my current <code>AsEnumerable</code> code to send it to the View as <code>IList</code>?</li>
  </ol>
  <p>Here is the code before any changes to make it <code>IList</code> were made:</p>
  <p>Get controller method:</p>
  <pre><code>public ActionResult Register_step6()
  {
      var wp = cg.GetUserWorkPoint();
      var model = (from p in db.WorkPointRoles
                   where p.WorkPoint == wp
                   select p).AsEnumerable()
         .Select(r =&gt; new RegisterEmployee_Step6Model()
     {
         Role = r.Role,
         isSelected = false,
         RoleDescription = cg.GetRoleDescription(r.Role),
         IconLocation = cg.GetRoleIconLocation(r.Role)
      });
  
      return View(model);
  }
  </code></pre>
  <p>Post controller method:</p>
  <pre><code>[HttpPost]
  public ActionResult Register_step6(IEnumerable&lt;RegisterEmployee_Step6Model&gt; model)
  {
      foreach (var i in model)
      {
          //code in here to update db
      }
  
      db.SaveChanges();
  }
  
          return RedirectToAction(&quot;Index&quot;);
      }
  }
  </code></pre>
  <p>View:</p>
  <pre><code>@model IEnumerable&lt;core_point.Models.RegisterEmployee_Step6Model&gt;
  
  @{
      ViewBag.Title = &quot;Index&quot;;
  }
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
  
      &lt;h2&gt;Select the roles that you have been employed to do, by clicking the check-box...&lt;/h2&gt;
  
      &lt;table class=&quot;table&quot;&gt;
          @foreach (var item in Model)
          {
              &lt;tr class=&quot;h3&quot;&gt;
                  &lt;td class=&quot;vert-align&quot;&gt;
                      &lt;img src=&quot;@item.IconLocation&quot; height=&quot;80&quot; width=&quot;80&quot; /&gt;
                  &lt;/td&gt;
  
                  &lt;td class=&quot;vert-align&quot;&gt;
                      @Html.CheckBoxFor(modelItem =&gt; item.isSelected, new { @class = &quot;mycheckBox&quot; })
                  &lt;/td&gt;
  
                  &lt;td class=&quot;vert-align&quot;&gt;
                      @Html.DisplayFor(modelItem =&gt; item.RoleDescription)
                  &lt;/td&gt;
  
                  &lt;td class=&quot;vert-align&quot;&gt;
                      @Html.TextBoxFor(modelItem =&gt; item.Role, new { hidden = &quot;hidden&quot; })
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
      
      &lt;/table&gt;
      &lt;div&gt;
          Roles selected:&lt;input type=&quot;number&quot; id=&quot;noSelected&quot; value=&quot;0&quot; /&gt;
      &lt;/div&gt;
  
      &lt;div class=&quot;form-group&quot;&gt;
          &lt;div class=&quot;col-md-offset-2 col-md-10&quot;&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Next&quot; class=&quot;btn btn-default&quot; /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;script src=&quot;~/Scripts/jquery-1.10.2.js&quot;&gt;&lt;/script&gt;
  
  &lt;script&gt;
      var fld_CheckBoxSelected = document.getElementById(&quot;CheckBoxSelected&quot;);
      var fld_noSelected = document.getElementById(&quot;noSelected&quot;);
  
      $(function(){
          $(&quot;input.mycheckBox&quot;).change(function(){
              var isSelected = this.checked;
  
              if (isSelected ) {
                  $(&quot;#noSelected&quot;).val( parseInt($(&quot;#noSelected&quot;).val())+ 1);
              }
              else
              {
                  $(&quot;#noSelected&quot;).val( parseInt($(&quot;#noSelected&quot;).val()) -  1);
              }
          });
  
      });
  
  &lt;/script&gt;
  </code></pre>
  <p>View model class:</p>
  <pre><code>public class RegisterEmployee_Step6Model
  {
      public byte Id { get; set; }
      public int? Role { get; set; }
      public bool isSelected { get; set; }
      public string RoleDescription { get; set; }
      public string IconLocation { get; set; }
  }
  </code></pre>