title: MVC5 view drop down list from ViewBag
body: <p>I'm new to MVC5/C# (fresh off a Silverlight project) and have a web application (not ASP.net) that I'm working on. I can't figure out how to get the value from a dropdown list that is populated from a ViewBag and not the model. Everything I've seen is geared towards ASP.NET and/or populating the dropdown from the model.</p>
  
  <p>I have this model for shifts:</p>
  
  <pre><code>   public class Shift
     {
        public Guid ShiftID { get; set; }
        public string AreaOfOperation { get; set; }
        public string UserName { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
     }
  </code></pre>
  
  <p>And this for AreaOfOperations:</p>
  
  <pre><code>   public class AreaOfOperations
     {
        public Guid AreaOfOperationsID { get; set; }
        public String AreaOfOperation { get; set; }
        public bool InUse { get; set; }      
     }
  </code></pre>
  
  <p>The relevant controller code, which populates the view nicely with a working dropdown:</p>
  
  <pre><code>  public ActionResult Create(DateTime? datetime)
    {
       List&lt;AreaOfOperations&gt; list = db.AreaOfOperations.Where(i =&gt; i.InUse == true).OrderBy(aoo =&gt; aoo.AreaOfOperation).ToList();
       ViewBag.DropDownAOOs = new SelectList(list, "AreaOfOperationsID", "AreaOfOperation");
  
       Shift shift = new Shift();
       shift.ShiftID = Guid.NewGuid();
       shift.StartTime = DateTime.Now;
       shift.UserName = User.Identity.Name;
       return View(shift);
    }
  
    // POST: Shifts/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "ShiftID,AreaOfOperations,UserName,StartTime")] Shift shift)
    {
       try
       {
          if (ModelState.IsValid)
          {
             shift.ShiftID = Guid.NewGuid();
             db.Shifts.Add(shift);
             db.SaveChanges();
             return RedirectToAction("Index");
          }
       }
       catch (DataException /* dex */)
       {
          //Log the error (uncomment dex variable name and add a line here to write a log.
          ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
       }
       return View(shift);
    }
  </code></pre>
  
  <p>And my view:</p>
  
  <pre><code>@model CRMgr5.Models.Shift
  
  @{
      ViewBag.Title = "Start Shift";
  }
  
  @using (Html.BeginForm()) 
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Shift&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.AreaOfOperations, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("AreaOfOperation", ViewBag.DropDownAOOs as SelectList, new { htmlAttributes = new { @class = "form-control" } })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.UserName, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.UserName, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.StartTime, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.StartTime, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.StartTime, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input id="btnStartShift" type="submit" value="Start Shift" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated. Thanks.</p>