title: Mvc - Form with viewmodel
body: <p>im working in mvc and have a form in my view which receive from my viewmodel a selectlist to insert the values into my dropdownlist.</p>
  
  <p>but on submit the form nothing work and i can´t figured it out what its going on.</p>
  
  <p>on submit the form i get null into my database.</p>
  
  <p>someone can help me out with this pls?</p>
  
  <p>here is my code:
  <strong>VIEW:</strong></p>
  
  <pre><code>model MyProject.Models.ViewModel
  @using(Html.BeginForm()) {
      @Html.DropDownListFor(o =&gt; o.cp.Companylist,Model.cp.Companylist)
       @Html.EditorFor(o =&gt; o.md.title)
       @Html.EditorFor(o =&gt; o.md.edition)
      @Html.EditorFor(o =&gt; o.md.number)
  
    &lt;input type="submit" name="submit" value="Add New"&gt;
  }
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>  public ActionResult Create()
          {   ViewModel vm = new ViewModel ();
              return View(vm);
          }
  
          //
          // POST: Create
  
          [HttpPost]
          public ActionResult Create(Mydata model)
          {
              if (ModelState.IsValid)
              {
                  db.Res.Add(model);
                  db.SaveChanges();
                  return RedirectToAction("Index","AdminPanel");
              }
  
              return View(model);
          }
  </code></pre>
  
  <p><strong>Model:</strong></p>
  
  <pre><code> public class ViewModel
      {
          public Mydata md { get; set; }
          public Company cp { get; set; }
      }
  </code></pre>
  
  <p><strong>Mydata - Model:</strong></p>
  
  <pre><code> public class Mydata
      {
          public int ID { get; set; }
          public string company_name{ get; set; }
          public string title{ get; set; }
          public string edition{ get; set; }
          public string number{ get; set; }
      }
  public class DefaultConnection : DbContext
      {
          public DbSet&lt;Mydata&gt; Res{ get; set; }
      }
  </code></pre>
  
  <p><strong>Companies - Model:</strong></p>
  
  <pre><code> public class Company
      {
          public int id { get; set; }
          public string name{ get; set; }
          public SelectList Companylist{ get; set; }  
  
          public Company()
          {
              Companylist= GetCompanies();
          }
  
          public SelectList GetCompanies()
          {
              var list = new List&lt;SelectListItem&gt;();
              string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
  
              using (var con = new SqlConnection(connection))
              {
                  con.Open();
                  using (var command = new SqlCommand("SELECT * FROM MyCompanies", con))
                  {
                      SqlDataReader reader = command.ExecuteReader();
                      while (reader.Read())
                      {
                          //string id = reader[0] as string;
                          string name_company= reader[1] as string;
                          list.Add(new SelectListItem() { Text = name_company, Value = name_company});
                      }
                  }
                  con.Close();
              }
              return new SelectList(list, "Value", "Text");
          }
  }
  </code></pre>