title: checkbox values in mvc
body: <p>I'm trying to figure how to properly represent values, process selected and later on edit form display checked items as checked and other possible values as nonchecked.</p>
  
  <p>I already try something but I'm not sure am I on the right track. With using these solution edit form only display checked item, and not showing other non checked.</p>
  
  <p><strong>If you have better solution please share, cause I'm trying to figure how these work.</strong></p>
  
  <p>Here it is: Assume there is User which may belong to one or many Roles. So I would represent these trough two viewmodels, UserViewModel and RoleViewModel</p>
  
  <pre><code>**UserViewModel.cs**
  
  public int id {get; set;}
  public string username {get; set;}
  
  public UserViewModel()
  {
     Roles = new List&lt;RoleViewModel&gt;();
  }
  
  public void SendToDomain(User user, ISession nhibSession)
  {
     if(user.Roles != null)
     {
        user.Roles.Clear();//Clear previous selected items
        foreach(Role role in Roles)
        {
          if(role.IsInRole)
            user.Role.Add(nhibSession.Load&lt;Role&gt;(user.RoleId)); 
        }
     }
  }
  
  **RoleViewModel**
  public bool IsInRole {get; set;}
  
  [HiddenInput(displayValue = false)]
  public int RoleId {get; set;}
  
  [HiddenInput(displayValue = true)]
  public string RoleName {get; set;}
  </code></pre>
  
  <p>And inside EditorTemplates I have RoleViewModel.cs</p>
  
  <pre><code>@model Models.RoleViewModel
  
  @Html.CheckBoxFor(m=&gt;m.IsInRole)
  @Html.HiddenFor(m=&gt;m.RoleId)
  @Html.LabelFor(m=&gt;m.IsInRole, Model.RoleName)
  </code></pre>
  
  <p>And finally inside Create view I display these like these
  @model Models.UserViewModel</p>
  
  <pre><code>&lt;div&gt; User roles &lt;/div&gt; &lt;div&gt; @Html.EditorFor(m =&gt; m.Roles) &lt;/div&gt;
  </code></pre>