title: Get Selected Items from CheckboxList MVC.NET Razor
body: <p>I have the following cshtml form </p>
  
  <pre><code>@using (Html.BeginForm(Html.BeginForm("Create", "UserRole", Model, FormMethod.Post)))
  {
     @Html.AntiForgeryToken()
     @Html.ValidationSummary(true)
  
     &lt;fieldset&gt;
         &lt;legend&gt;Role&lt;/legend&gt;
  
         &lt;div class="editor-label"&gt;
             @Html.Label(Model.User.UserName)
         &lt;/div&gt;
         &lt;div class="editor-field"&gt;
             @Html.CheckBoxList(Model.CheckboxList)
         &lt;/div&gt;
  
         &lt;p&gt;
             &lt;input type="submit" value="Create" /&gt;
         &lt;/p&gt;
     &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>And I wish to get the Model.CheckboxList selected Items in my action.</p>
  
  <p>I have the following Create Action in my Controller</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Create(UserRoleViewModel userRoleViewModel)
      {
          if (ModelState.IsValid)
          {
              //_context.Role.Add(role);
              //_context.SaveChanges();
              //return RedirectToAction("Index");
          }
  
          return View(viewModel);
      }
  </code></pre>
  
  <p>However the viewModel.CheckboxList is 0.</p>
  
  <p>How can I pass the selected values of the checkboxlist, and also the Model.User to the Controller Action?</p>
  
  <p>My ViewModel looks like this :-</p>
  
  <pre><code>    public User User { get; set; }
      public IEnumerable&lt;Role&gt; RoleList { get; set; }
  
      public List&lt;UserRoleViewModel&gt; UserList { get; set; }
  
      public IEnumerable&lt;SelectListItem&gt; CheckboxList { get; set; }
  
      public UserRoleViewModel()
      {
      }
  
      public UserRoleViewModel(User user, IEnumerable&lt;Role&gt; roleList )
      {
          User = user;
          RoleList = roleList;
      }
  </code></pre>
  
  <p>Thanks for your help and time!</p>
  
  <p>UPDATE ----------- After reading this post <a href="https://stackoverflow.com/questions/5284395/checkboxlist-in-mvc3-view-and-get-the-checked-items-passed-to-the-controller">enter link description here</a>, I tried to adapt my code to follow the example, but I am still finding problems with this updated code.</p>
  
  <p>Now I have the following :-</p>
  
  <p>cshtml :-</p>
  
  <pre><code>@model IEnumerable&lt;MvcMembership.ViewModels.RoleCheckboxListViewModel&gt;
  
      @using (Html.BeginForm())
      {
          @Html.EditorForModel()
          &lt;input type="submit" value="OK" /&gt;
      }
  </code></pre>
  
  <p>Views/Role/EditorTemplates/RoleCheckboxListViewModel.cshtml</p>
  
  <pre><code>@model MvcMembership.ViewModels.RoleCheckboxListViewModel
  @Html.HiddenFor(x =&gt; x.RoleId)
  @Html.HiddenFor(x =&gt; x.RoleName)
  &lt;div&gt;
     @Html.CheckBoxFor(x =&gt; x.Checked)
     @Html.LabelFor(x =&gt; x.Checked, Model.RoleName)
  &lt;/div&gt;
  </code></pre>
  
  <p>ViewModels :-</p>
  
  <pre><code>public class RoleCheckboxListViewModel
  {
      public string RoleId { get; set; }
      public string RoleName { get; set; }
      public bool Checked { get; set; }
  }
  </code></pre>
  
  <p>and the controller action is as follows :-</p>
  
  <pre><code>        public ActionResult Create(int? uid)
      {
          var checkBoxList = new[]
          {
              new RoleCheckboxListViewModel() { 
                      RoleId = "1", Checked = true, RoleName = "item 1"  },
              new RoleCheckboxListViewModel() { 
                      RoleId = "2", Checked = true, RoleName = "item 2" },
              new RoleCheckboxListViewModel() { 
                      RoleId = "3", Checked = true, RoleName = "item 3" },
          };
  
          return View(checkBoxList);
      }
  </code></pre>
  
  <p>The problem I have now is that on the Create.cshtml. I cannot see the checkboxlist, but only 123 displayed as well as the OK button.</p>
  
  <p>Any help would be very much appreciated cause I am at a dead end at the moment.</p>