title: Pass selected value in dropdownlist from view to controller when form post
body: <p>I have these models and a view model. When form posting to create the entries, the selected value for <code>WardID</code> and  <code>DoctorID</code> becomes zero in the controllers POST method.</p>
  
  <p>View code </p>
  
  <pre><code>@model NewClient.HospitalMgt.ViewModel.PatientAdmissionViewModel
  .... 
  @using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Admission.WardID, "Ward")
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.DropDownList("WardID", String.Empty)
              @Html.ValidationMessageFor(model =&gt; model.Admission.WardID)
          &lt;/div&gt;
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Admission.DoctorID, "Doctor")
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.DropDownList("DoctorID", String.Empty)
              @Html.ValidationMessageFor(model =&gt; model.Admission.DoctorID)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Patient.PatientName)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Patient.PatientName)
              @Html.ValidationMessageFor(model =&gt; model.Patient.PatientName)
          &lt;/div&gt;
          .... //more controls for properties of Patient
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Admission.AdmissionNumber)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Admission.AdmissionNumber)
              @Html.ValidationMessageFor(model =&gt; model.Admission.AdmissionNumber)
          &lt;/div&gt;
          .... //more controls for properties of Admission
          &lt;p&gt;
              &lt;input type="submit" value="Create" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>Controller code </p>
  
  <pre><code>public ActionResult Create()
  {
      ViewBag.WardID = new SelectList(db.Wards, "WardID", "WardNumber");
      ViewBag.DoctorID = new SelectList(db.Doctors, "DoctorID", "DoctorName");
      return View();
  }
  
  [HttpPost]
  [ValidateAntiForgeryToken]
  public ActionResult Create(PatientAdmissionViewModel Id)
  {
      if (ModelState.IsValid)
      {
          var c = new Patient()
          {
              Address = Id.Patient.Address,
              ContactNumber = Id.Patient.ContactNumber,
              EmergencyContact = Id.Patient.EmergencyContact,
              PatientName = Id.Patient.PatientName,
          };
          var a = new Admission()
          {
              AdmissionNumber = Id.Admission.AdmissionNumber,
              AdmissionDate = Id.Admission.AdmissionDate,
              **WardID = Id.Admission.WardID, //becomes zero 
              DoctorID = Id.Admission.DoctorID //becomes zero** 
          };
          db.Patients.Add(c);
          db.Admissions.Add(a);
          db.SaveChanges();
          return RedirectToAction("Index");
      }
  
      ViewBag.WardID = new SelectList(db.Wards, "WardID", "WardNumber", Id.Admission.WardID);
      ViewBag.DoctorID = new SelectList(db.Doctors, "DoctorID", "DoctorName", Id.Admission.DoctorID);
      return View(Id);
  }
  </code></pre>
  
  <p>Models and View models </p>
  
  <pre><code>//[NotMapped]
  public class PatientAdmissionViewModel 
  {
      public Patient Patient { get; set; }
      public Admission Admission { get; set; }
  }
  
  public class Patient
  {
      public int PatientID { get; set; }
      [Required(ErrorMessage = "A Patient Name is required")]
      public string PatientName { get; set; }
      public string Address { get; set; }
      [Required(ErrorMessage = "A Contact Number is required")]
      public string ContactNumber { get; set; }
      public string EmergencyContact { get; set; }
      public virtual Ward Ward { get; set; }
  }
  
  public class Admission
  {
      public int AdmissionID { get; set; }
      public string AdmissionNumber { get; set; }
      public Nullable&lt;int&gt; PatientID { get; set; }
      public int WardID { get; set; }
      public int DoctorID { get; set; }
      public System.DateTime AdmissionDate { get; set; }
      public virtual Ward Ward { get; set; }
      public virtual ICollection&lt;PatientPayment&gt; PatientPayments { get; set; }
  }
  </code></pre>