title: Setting selected property in SelectList for each object in collection based on selected ID in ASP.NET MVC 3
body: <p>Follow up question to :
  <a href="https://stackoverflow.com/questions/13713119/populate-a-list-property-from-delimited-string/13713238#13713238">Populate a list property from delimited string</a></p>
  
  <p>The problem I'm having is the DropDown boxes in my Edit view are not retaining/selecting the the proper item based on the AssignedUserID property within BugAssignment model.</p>
  
  <p>Models:</p>
  
  <pre><code>public class BugAssignment
  {
      public int BugAssignmentID { get; set; }
      public int BugNumber { get; set; }
      public int? AssignedUserID { get; set; }
      public virtual User AssignedUser { get; set; }
      public virtual Status Status { get; set; }
      public IEnumerable&lt;SelectListItem&gt; Users { get; set; }
  }
  
  public class BugAssignmentList
  {
      public BugAssignmentList()
      {
          BugAssignments = new List&lt;BugAssignment&gt;();
      }
      public int BugAssignmentListID { get; set; }
      public string Name { get; set; }
      public List&lt;BugAssignment&gt; BugAssignments { get; set; }
  }
  </code></pre>
  
  <p>Edit/Details ViewModel</p>
  
  <pre><code>public class BugAssignmentListDetailsViewModel
  {
      public int BugAssignmentListID { get; set; }
      public string Name { get; set;}
      public List&lt;BugAssignment&gt; BugAssignments { get; set; }
  }
  </code></pre>
  
  <p>I have the following two methods in my controller:</p>
  
  <pre><code>    public ActionResult Edit(int id)
      {
          var balData = db.BugAssignmentLists.Include(bn =&gt; bn.BugAssignments).ToList();
          //this is a reference to the bug assignment list object whose ID was passed in
          BugAssignmentList bugAssignmentList = balData.Where(b =&gt; b.BugAssignmentListID == id).FirstOrDefault();
          //loop through the bug assignment list and for each bug assignment
          foreach (BugAssignment b in bugAssignmentList.BugAssignments)
          {
              //create the select list for each BugAssignment object
              b.Users = users.Select(u =&gt; new SelectListItem
              {
                  Value = u.UserID.ToString(),
                  Text = u.FullName,
                  Selected = u.UserID == b.AssignedUserID //&lt;&lt;THIS IS WHERE IT IS MESSING UP
              });
  
          }
          BugAssignmentListDetailsViewModel detailsVM = new BugAssignmentListDetailsViewModel
          {
              BugAssignmentListID = id
          };
          if (bugAssignmentList != null)
          {
              detailsVM.BugAssignments = bugAssignmentList.BugAssignments;
              detailsVM.Name = bugAssignmentList.Name;
          }
          return View(detailsVM);
      }
  
      // POST
      [HttpPost]
      public ActionResult Edit(BugAssignmentListDetailsViewModel viewModel)
      {
          var balData = db.BugAssignmentLists.Include(bn =&gt; bn.BugAssignments).ToList();
          var users = db.Users.ToList();
          BugAssignmentList bugAssignmentList = balData.Where(b =&gt; b.BugAssignmentListID == viewModel.BugAssignmentListID).FirstOrDefault();
          bugAssignmentList.Name = viewModel.Name;
          bugAssignmentList.BugAssignments = viewModel.BugAssignments;
  
          if (ModelState.IsValid)
          {
              UpdateModel(bugAssignmentList, "BugAssignmentList");
              db.Entry(bugAssignmentList).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(bugAssignmentList);
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;table&gt;    
  &lt;thead&gt;
      &lt;th&gt;
          Bug Number
      &lt;/th&gt;
      &lt;th&gt;
          Assigned User
      &lt;/th&gt;
  &lt;/thead&gt;
      @for(int i=0; i&lt;Model.BugAssignments.Count(); i++)
      {
          &lt;tr&gt;
              &lt;td style="text-align: center;"&gt;
                  @Html.EditorFor(modelItem =&gt; Model.BugAssignments[i].BugNumber)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DropDownListFor(modelItem =&gt; Model.BugAssignments[i].AssignedUserID, Model.BugAssignments[i].Users, "-- Select User --")
                  @Html.HiddenFor(modelItem =&gt; Model.BugAssignments[i].BugAssignmentID)
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  &lt;/table&gt;
  </code></pre>
  
  <p>The POST is working fine. Looking in the database, each BugAssignment in the BugAssignments collection within BugAssignmentList is updating with the proper AssignedUserID from the dropdown box in my Edit view, so it's not the POST method.</p>
  
  <p>The problem is in the Edit(int id). What's happening is very odd. I've put a breakpoint right before the foreach loop and I looked inside of bugList.BugAssignments. All the AssignedUserID's are correct, but after the loop completes, and I inspect the list again, all the SelectListItems in BugAssignment.Users have the same User selected. </p>
  
  <p>What is wrong with the following code that would cause this?</p>
  
  <pre><code>    foreach (BugAssignment b in bugAssignmentList.BugAssignments)
      {
          //create the select list for each BugAssignment object
          b.Users = users.Select(u =&gt; new SelectListItem
          {
              Value = u.UserID.ToString(),
              Text = u.FullName,
              Selected = u.UserID == b.AssignedUserID //&lt;&lt;THIS IS WHERE IT IS MESSING UP
          });
  
      }
  </code></pre>
  
  <p>Also, as a side note: I want the interface to look something like this (which is why I need the SelectList to be on the BugAssignment model, unless there is an easier way. I don't want to write a BugAssignment controller/view I want someone to be able to quickly assign users to bug numbers like the following:
  <img src="https://i.stack.imgur.com/6Timp.jpg" alt="enter image description here"></p>
  
  <p>Here is another screenshot that better shows what is happening:</p>
  
  <p><img src="https://i.stack.imgur.com/wWMfW.png" alt="enter image description here"></p>