title: How do I specify the selected value using DropdownListFor with a ViewBag?
body: <p>In my view I have the following DropDownListFor: </p>
  
  <pre><code>@Html.DropDownListFor(m =&gt; item.BillingStatusID, (SelectList)ViewBag.BillingStatusID, "Select...")
  </code></pre>
  
  <p>I am using the following controller, which is used to both edit and create:</p>
  
  <pre><code>[HttpGet]
  public ActionResult Create()
  {
      var week = new Week();
      ApplicationDbContext db = new ApplicationDbContext();
      var timeEntries = from te in db.TimeEntries
                        select te;
  
      ViewBag.BillingStatusID = new SelectList(db.BillingStatus, "BillingStatusID", "Name", row.BillingStatusID);
  
      foreach (TimeEntry t in timeEntries)
      {
          week.TimeEntries.Add(t);
      }
  
      week.TimeEntries.Add(new TimeEntry { TimeEntryID = -1 });
  
      return View(week);
  
  }
  </code></pre>
  
  <p>However, when the page renders it shows the selected value as "Select..." instead of the billing status for that item.</p>
  
  <p>The desired result is for the page to render like this:</p>
  
  <p><a href="https://i.stack.imgur.com/ju73Z.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ju73Z.jpg" alt="enter image description here"></a></p>
  
  <p>But instead it renders like this:</p>
  
  <p><a href="https://i.stack.imgur.com/C3ypa.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C3ypa.jpg" alt="enter image description here"></a></p>
  
  <p>If I change the ViewBag to:</p>
  
  <pre><code> ViewBag.BillingStatusID = new SelectList(db.BillingStatus, "BillingStatusID", "Name", 2);
  </code></pre>
  
  <p>for example, it works and the DropDownListfor is populated with the entry with BillingStatusID = 2, but obviously this isn't the desired behaviour. How can I ensure that the index is returned properly from the ViewBag?</p>
  
  <p>My suspicion is that I should be somehow passing a variable back to the controller, but I'm new to MVC, so not sure how this should work.</p>