title: MVC ViewBag dropdown values parameter passing
body: <p>In database I have table user and student. I generated model using Entity Framework. Since this student class generated only had fields from student, but not form user, I created my own student class with all this fields to use like viewmodel.</p>
  
  <p>I'm using my ViewBag to pass data for dropdown in my view.</p>
  
  <pre><code>ViewBag.Programs = Entities.Programs;
  </code></pre>
  
  <p>Programs are list study programs at university (like: computer science, law, etc...)
  So it is 1:N relationship where student can be at just one program, but program can be studied by more students.
  And in my view I use DropDownFor to make dropdown list from which program is assigned to students when new student is added to database. </p>
  
  <pre><code>&lt;div class="editor-field"&gt;
              @Html.DropDownListFor(model =&gt; model.ProgramID, ((IEnumerable&lt;SSVN.Models.Program&gt;)ViewBag.Programs).Select(option =&gt; new SelectListItem 
         {
          Text = (option == null ? "None" : option.Name), 
          Value = option.ID.ToString(),
          Selected = (Model != null) &amp;&amp; (option.ID == Model.ID)
         }), "Choose...")
              @Html.ValidationMessageFor(model =&gt; model.ProgramID)
          &lt;/div&gt;
  </code></pre>
  
  <p>Problem is when I click on form submit button, this error appears:</p>
  
  <p>Exception Details: System.ArgumentNullException: Value cannot be null.
  Parameter name: source</p>
  
  <pre><code>@Html.DropDownListFor(model =&gt; model.ProgramID, ((IEnumerable&lt;SSVN.Models.Program&gt;)ViewBag.Programs).Select(option =&gt; new SelectListItem 
  </code></pre>
  
  <p>This part <code>model =&gt; model.ProgramID</code>   makes error</p>
  
  <p>Well, I tried to simplify problem so that there is no other things that might be unclear to you, so I translated my code to English, since the real code is more on my native language.
  I think error is there because that is what browser says. Here is code at it's original:</p>
  
  <p>EDIT GET ACTION</p>
  
  <pre><code>// GET: /Unija/Studenti/Izmeni
  
          public ActionResult Izmeni(int ID)
          {
              ViewBag.StudProgrami = Entities.Studijski_Program;
  
              SSVN.ModelView.Student student = new SSVN.ModelView.Student();
              SSVN.Models.Student stud = Entities.Students.Single(x =&gt; x.ID == ID);
              Korisnik korisnik = Entities.Korisniks.Single(s =&gt; s.ID == ID);
              student.ID = korisnik.ID;
              student.KorisnickoIme = korisnik.Korisnicko_Ime;
              student.Lozinka = korisnik.Lozinka;
              student.Ime = stud.Ime;
              student.Prezime = stud.Prezime;
              student.BrojIndeksa = stud.Broj_Indeksa;
              student.TrenutniSemestar = stud.Trenutni_Semestar;
              student.StudijskiProgramID = stud.Studijski_Program_ID;
  
              return View(student);
          }
  </code></pre>
  
  <p>EDIT POST ACTION</p>
  
  <pre><code>// POST: /Unija/Studenti/Izmeni
  
          [HttpPost]
          public ActionResult Izmeni(SSVN.ModelView.Student student)
          {
              if (ModelState.IsValid)
              {
                  Korisnik korisnikToUpdate = Entities.Korisniks.Single(x =&gt; x.ID == student.ID);
                  korisnikToUpdate.Korisnicko_Ime = student.KorisnickoIme;
                  korisnikToUpdate.Uloga_ID = Entities.Ulogas.SingleOrDefault(x =&gt; x.Naziv == "Student").ID;
                  Entities.SaveChanges();
  
                  SSVN.Models.Student studentToUpdate = Entities.Students.Single(x =&gt; x.ID == student.ID);
                  studentToUpdate.Ime = student.Ime;
                  studentToUpdate.Prezime = student.Prezime;
                  studentToUpdate.Broj_Indeksa = student.BrojIndeksa;
                  studentToUpdate.Trenutni_Semestar = student.TrenutniSemestar;
                  studentToUpdate.Studijski_Program_ID = student.StudijskiProgramID;
                  Entities.SaveChanges();
  
                  return RedirectToAction("Index");
              }
  
              return View(student);
          }
  </code></pre>
  
  <p>VIEW</p>
  
  <pre><code>@model SSVN.ModelView.Student
  
  @{
      ViewBag.Title = "Izmeni";
  }
  
  &lt;h2&gt;Dodaj&lt;/h2&gt;
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;Student&lt;/legend&gt;
  
          &lt;div class="hidden-field"&gt;
          @Html.HiddenFor(model =&gt; model.ID)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.KorisnickoIme)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.KorisnickoIme)
              @Html.ValidationMessageFor(model =&gt; model.KorisnickoIme)
          &lt;/div&gt;
          &lt;input type="hidden" name="Lozinka" value="lozinka" /&gt;
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Ime)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Ime)
              @Html.ValidationMessageFor(model =&gt; model.Ime)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Prezime)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Prezime)
              @Html.ValidationMessageFor(model =&gt; model.Prezime)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.BrojIndeksa)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.BrojIndeksa)
              @Html.ValidationMessageFor(model =&gt; model.BrojIndeksa)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.TrenutniSemestar)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.TrenutniSemestar)
              @Html.ValidationMessageFor(model =&gt; model.TrenutniSemestar)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.StudijskiProgramID)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.DropDownListFor(model =&gt; model.StudijskiProgramID, ((IEnumerable&lt;SSVN.Models.Studijski_Program&gt;)ViewBag.StudProgrami).Select(option =&gt; new SelectListItem 
         {
          Text = (option == null ? "None" : option.Naziv), 
          Value = option.ID.ToString(),
          Selected = (Model != null) &amp;&amp; (option.ID == Model.ID)
         }), "Izaberite")
              @Html.ValidationMessageFor(model =&gt; model.StudijskiProgramID)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Izmeni" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>