title: ASP.NET MVC 4 Saving Model List Items in Edit View
body: <p>I'm trying to save below project Model</p>
  
  <pre><code>public partial class Project: BaseModel
  {
      public Project()
      {
          ProjectUsers = new List&lt;ProjUser&gt;();
      }
  
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      [Display(Name="Project ID")]
      public int ProjectID { get; set; }
  
      [Required]
      public string Title { get; set; }
      [Display(Name = "Users")]
      public virtual List&lt;ProjUser&gt; ProjectUsers { get; set; }
  }
  </code></pre>
  
  <p>ProjUser Model</p>
  
  <pre><code>public partial class ProjUser: BaseModel
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int ProjUserID { get; set; }
  
      public int UserID { get; set; }
  
      public int ProjectID { get; set; }
      [ForeignKey("ProjectID")]
      public virtual Project Project { get; set; }
  
      public bool? Signatories { get; set; }
  
  }
  </code></pre>
  
  <p>The Users are populated via a separate database and the link between the project and user are maintains via the ProjUser model, UserID value.</p>
  
  <pre><code>public partial class User : BaseModel
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int UserID { get; set; }
      [Required]
      [Display(Name = "dispUserName" , ResourceType = typeof(Resource))]
      public string UserName { get; set; }
  }
  </code></pre>
  
  <p>In the view i'm populating the list of users as a selectable (JqueryUI) and selecting the users who are available in the project.</p>
  
  <pre><code>@model Impetro.Models.Project
  @{
      ViewBag.Title = "ProjectSettings";
  }
  
  &lt;h2&gt;@ViewBag.Title&lt;/h2&gt;
  @using (Html.BeginForm())
  {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;Project Settings&lt;/legend&gt;
          @Html.HiddenFor(model =&gt; model.ProjectID)
          &lt;div&gt;
              &lt;div class="editor-label"&gt;
                  @Html.LabelFor(model =&gt; model.Title)
              &lt;/div&gt;
              &lt;div class="editor-field"&gt;
                  @Html.EditorFor(model =&gt; model.Title)
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/fieldset&gt;
  &lt;p&gt;
      &lt;input type="submit" value="Save" /&gt;
  &lt;/p&gt;
  }
  &lt;h3&gt;@Html.DisplayNameFor(model =&gt; model.ProjectUsers)&lt;/h3&gt;
  @{
      var pojectUserIDs = from pu in Model.ProjectUsers
                          select new { pu.UserID };
  
      String userIDstr = "";
      foreach (var user in pojectUserIDs)
      {
          userIDstr = userIDstr + "#" + user.UserID.ToString() + ";";
      }
  }
  @Html.Hidden("userValues", userIDstr, "id='userValues'")
  &lt;table data-impetro-uitype="selectable" data-impetro-selectorvalues="#userValues" id="projUserGrd"&gt;
      @foreach (var user in ViewBag.Users as IEnumerable&lt;Impetro.Models.Global.User&gt;)
      {
          &lt;tr id="@user.UserID" class="ui-widget-content"&gt;&lt;td&gt;@user.UserName&lt;/td&gt; &lt;td&gt;@Html.CheckBox("chk" + @user.UserID) @Html.Label("chk" + @user.UserID, "PO Signatory")&lt;/td&gt;&lt;/tr&gt;
      }
  &lt;/table&gt;
  </code></pre>
  
  <p>The JS to convert the above table to a selectable and to select the already added users.</p>
  
  <pre><code>$(function () {
      $("[data-impetro-uitype='selectable']").bind("mousedown", function (e) {
          e.metaKey = true;
      })
      .selectable({
          filter: "tr",
          create: function (event, ui) {
              var selectable = $(this);
              var valueEle = selectable.attr("data-impetro-selectorvalues");
              var valArr = $(valueEle).val().split(";");
              for (var i = 0; i &lt; valArr.length; i++) {
                  if (valArr[i] == "") {
                      continue;
                  }
                  var id = valArr[i];
                  selectable.find(id).addClass("ui-selected");
              }
          }
      });
  });
  </code></pre>
  
  <p>How can the changes to the projusers list be captured in the model?
  Btw: Is there an easier method to select the values on initial load itself, which supports tracking the projUser list changes</p>
  
  <p>Thanks in Advance</p>
  
  <p>As Requested by lnanikian here is the GET Action for the controller. I still did not write the POST Action for this, becos im still in doubt about how to proceed with saving the details into the list.</p>
  
  <pre><code>public ActionResult ProjectSettings(int id)
      {
          ViewBag.Users = userRepository.All;
          return View(projectRepository.All
                 .Where(p =&gt; p.ProjectID == id).SingleOrDefault());
      }
  </code></pre>