title: Populate dropdownlist from database in asp.net MVC in a form
body: <p>Ok, I found some similar posts, but I don't find a way to solve it without a little help (the other posts have incomplete dates).</p>
  
  <p>I have a page where I need to display a dropdownlist with values from database (display Name, but keeping the key Id as value).... this list, with other types of input for a form.</p>
  
  <p>My Model:</p>
  
  <pre><code>public class AddOfferAnnounceModel
  {
      public AnnounceTypeList AnnounceTypeList {get; set; }
      public int Surface { get; set; }
      public int SubTypeId { get; set; }
  
      [Required]
      public decimal Price { get; set; }
  
      [StringLength(200, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 0)]
      public string AnnounceDetails { get; set; }
  
      public bool Net { get; set; }
  }
  
  public class AnnounceTypeList
  {
      public int SubTypeId { get; set; }
      public string TypeDesc { get; set; }
  }
  </code></pre>
  
  <p>My Controller [httpGET] Action (where probably must populate the dropdownlist:</p>
  
  <pre><code>// GET: Announce/Add new announce
      public ActionResult AddOffer()
      {
          string sql = "select typeId, typeDesc from announceTypes where parentTypeId = 1";
          using (SqlConnection cn = new SqlConnection(ConnectionString))
          {
              cn.Open();
              DataTable dt = new DataTable();
              SqlCommand cmd = new SqlCommand(sql, cn);
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              da.Fill(dt);
  
              var apartmentTypeList = new List&lt;AnnounceTypeList&gt;();
              foreach (DataRow dr in dt.Rows)
              {
                  var apartmentType = new AnnounceTypeList
                  {
                      SubTypeId = Convert.ToInt32(dr["TypeId"]),
                      TypeDesc = dr["TypeDesc"].ToString()
                  };
                  apartmentTypeList.Add(apartmentType);
              }
              return View();
          }
      }
  </code></pre>
  
  <p>My Controller [httpPOST] - where are all values returned from View and the query to insert into database </p>
  
  <pre><code>[HttpPost]
      public ActionResult AddOffer(AddOfferAnnounceModel model)
      {
          bool isValid = true; // daca datele introduse in formularul anuntului sunt corecte si respecta regulile
  
          if (isValid)
          {
              string announceDetailsItem = model.AnnounceDetails;
              decimal priceItem = model.Price;
              int surfaceItem = model.Surface;
              int subTypeId = model.SubTypeId; // GETTING FROM DROPDOWNLIST !!!!
              int netItem = Convert.ToInt32(model.Net);
              DateTime dateItem = DateTime.Now;
  
              string sql = "insert into announces (typeid, statusId, locationId, finalPrice, date, surface, net, announceDetails) values (1, 1, 1, " + priceItem + ",'" + dateItem + "'," + surfaceItem + "," + netItem + ",'" + announceDetailsItem + "')";
                    sql += " insert into announceOfferApartments (announceId, subTypeId, yPersonTypeId) values ((select max(announceId) from announces), 6, 4)";
  
              using (SqlConnection cn = new SqlConnection(ConnectionString))
              {
                  cn.Open();
                  SqlCommand cmd = new SqlCommand(sql, cn)
                  {
                      CommandType = CommandType.Text
                  };
                  cmd.ExecuteScalar();
                  return RedirectToAction("OfferList", "Announce");
                  // daca datele au fost introduse in DB, se redirectioneaza catre Lista Anunturilor
              }
          }
          return OfferList();
      }
  </code></pre>
  
  <p>My View :</p>
  
  <pre><code>@model myApp.com.Models.AddOfferAnnounceModel
  </code></pre>
  
  <p>...</p>
  
  <pre><code>&lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt; model.Price, new { htmlAttributes = new { @class = "form-control" } })
              @Html.ValidationMessageFor(model =&gt; model.Price, "", new { @class = "text-danger" })
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="form-group"&gt;
          @Html.LabelFor(model =&gt; model.Surface, htmlAttributes: new { @class = "control-label col-md-2" })
          &lt;div class="col-md-10"&gt;
              @Html.EditorFor(model =&gt; model.Surface, new { htmlAttributes = new { @class = "form-control" } })
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>....</p>
  
  <p>I know can't have more than one model in a View. I know that probably must create a Combined class which include all parameters, but I don't know how I must to populate a List from DataTable (or maybe a Dictionary with Key and Value ?!?) and display the Value in dropdown, and transfer the key to parameter subTypeId in Controller->ActionResult AddOffer[httpPost].</p>
  
  <p>I need to create 4 5 dropdownlists in same form, but probably the method will be the same .... !</p>
  
  <p>thanks, and hope to not receive too many negative votes :D </p>