title: Retrieving dynamic checkbox values and label from strongly typed view in MVC3
body: <p>EDIT: whoever fixed my question so the code displayed right, thank you! :)</p>
  
  <p>I have a silly problem I really need som help with, after a whole day of googling I'm close to loosing my head!</p>
  
  <p>I am working with the asp.net membership, and I want to be able to set roles on user once the web app is upp and running.</p>
  
  <p>To implement this I am sending a model to a strongly typed view with a list of checkboxes, if the user is in one of the roles in the list the checkbox is checked. (this part works)</p>
  
  <p>But I cant figure out how to return the checkbox values in the Edit method, as the model returns 'null' on the List-property. </p>
  
  <p>I am sure I've missed something obvious here, and would be very happy for any help...</p>
  
  <p>And how can I add code to this question? I cant get the formatting right...</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model Mvc4m.Models.UserRoles
  @{
      ViewBag.Title = "Edit";
  }
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;    &lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  
  @using (Html.BeginForm()) {
  @Html.ValidationSummary(true)
  &lt;fieldset&gt;
      &lt;legend&gt;UserRoles&lt;/legend&gt;
  
      &lt;div class="editor-label"&gt;
          &lt;h2&gt; @Html.DisplayFor(model =&gt; model.Name)&lt;/h2&gt;
      &lt;/div&gt;
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.Roles)
      &lt;/div&gt;
      &lt;div class="editor-field" id="testar"&gt;
  
      @foreach (var model in Model.AllRolles)
      {
          @Html.CheckBoxFor(item =&gt; model.IsActive, model.Role)
          @Html.Label(model.Role)  
      }
  
      &lt;/div&gt;
  
      &lt;div class="editor-label"&gt;
          @Html.LabelFor(model =&gt; model.IsApproved)
      &lt;/div&gt;
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.IsApproved)
          @Html.ValidationMessageFor(model =&gt; model.IsApproved)
      &lt;/div&gt;
  
      &lt;p&gt;
          &lt;input type="submit" value="Save" /&gt;
      &lt;/p&gt;
  &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public List&lt;AllRolles&gt; UserActiveRoles(string name)
  {
      var list = new List&lt;AllRolles&gt;();
  
      foreach(var role in Roles.GetAllRoles())
      {
          var hej = new AllRolles()
                        {
                            Role = role,
                            IsActive = Roles.IsUserInRole(name,role)
                        };
          list.Add(hej);
      }
      return list;
  }
  
  public ActionResult Index()
  {
      var roles = Roles.GetAllRoles();
      var users = from MembershipUser u in Membership.GetAllUsers()
                  select new UserRoles()
                  {
                      Name = u.UserName,
                      AllRolles = UserActiveRoles(u.UserName)
                  };
      return View(users);
  }
  
  public ActionResult Edit(string name)
  {
      var user = Membership.GetUser(name);
      var model = new UserRoles()
      {
          Name = user.UserName,
          AllRolles = UserActiveRoles(name)
      };
      return View(model);
  }
  
  //
  // POST: /Admin/ManageUsers/Edit/5
  
  [HttpPost]
  public ActionResult Edit(UserRoles user)
  {
      var name = user.Name;
  
      foreach (var role in user.AllRolles)
      {
          if (role.IsActive == true)
              Roles.AddUserToRole(name,role.Role);
          else 
              Roles.RemoveUserFromRole(name,role.Role);
      }
  
      return RedirectToAction("Index");
  }
  </code></pre>
  
  <p><strong>Models</strong></p>
  
  <pre><code>public class UserRoles
  {
      public string Name { get; set; }
  
      public List&lt;string&gt; Roles { get; set; }
  
      public bool IsApproved { get; set; }
  
      public List&lt;AllRolles&gt; AllRolles { get; set; }
  
  }
  
  namespace Mvc4m.Areas.Admin.Models
  {
      public class AllRolles
      {
          public string Role { get; set; }
          public bool IsActive { get; set; }
      }
  }
  </code></pre>