title: MVC combobox and how to save it in database?
body: <p>I am a beginner in MVC (but not in C#), but the project must be made in Visual Studio .NET app (MVC or WPF) using Entity Framework. I made a Database First app, but here is my problem:</p>
  
  <p>In table Customers<br/>
  I have Id, Title, Name, MiddleName, Surname, Address, CountryId(relationship to Table Countries[Id, Name] 1 to many), Active(bit, True/False, Checkbox)</p>
  
  <p>In table Countries<br/>
  Id, Name(predefined)</p>
  
  <p>Another table Titles(that I made, because I couldn't hardcode dropdownlists)<br/>
  Id, TitleName</p>
  
  <p>My question is when I created the Controller + View, the Title is displayed as TextBox(aka @Html.Editor(...)), when I change it to <code>@Html.DropDownList("nameHere", null, new { htmlAttributes = new { @class = "form-control" } })</code></p>
  
  <p>Then in my GET I create a dropdown view list:</p>
  
  <pre><code>public ActionResult Create()
          {
             ViewBag.nameHere = new SelectList(db.Titles, "Id", "TitleName");
              ViewBag.CountryId = new SelectList(db.Countries, "Id", "Name");
              return View();
          }
  </code></pre>
  
  <p>Then in my POST I put it too:</p>
  
  <pre><code>[HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create([Bind(Include = "Id,Title,Name,MiddleName,Surname,Address,CountryId,Active")] Customer customer)
          {
              if (ModelState.IsValid)
              {
                  db.Customers.Add(customer);
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
              ViewBag.nameHere = new SelectList(db.Titles, "Id", "TitleName", db.Title); //selects from table Titles the Id and TitleName for the field Title in Customers table? at least that's how I interpretate it
              ViewBag.CountryId = new SelectList(db.Countries, "Id", "Name", customer.CountryId); //same for Country
              return View(customer);
          }
  </code></pre>
  
  <p>But I have 3 problems:</p>
  
  <p>1st one - the server returns for Title in db.Customers NULL value<br/>
  2nd one - if I don't use dropdown then it will send the value from the TextBox field(but people can write whatever they want)<br/>
  3rd one(when using ViewBag and ViewData) - There is no ViewData item of type 'IEnumerable' that has the key "nameHere"</p>
  
  <p>I just want a simple dropdown list where I have 4 options - Ms, Mrs, Mr, Miss and when I select it and send the full form to the server(Name, MiddleName, Surname, etc), in the table Customers to save the string from the dropdown list. nothing more.</p>
  
  <p>I made my db with Database First(aka creating db then Model, not around)</p>