title: Dropdownlist selection saving id instead of name
body: <p>I am new to MVC and jQuery.  The following issue involves two tables, Product and QuoteDetail.  I am using a DropDownListFor to allow the user to select products from the Product table based upon Name.  Once the Name, is selected, two textboxes in the QuoteDetail record are populated with the ProductId, and the Price, respectively.  All that is working great.  My problem is that when I save the QuoteDetail record, it saves the ProductId in the ProductName field instead of the ProductName.  I don't see how I can get away from using the ProductId in the SelectList, as I need it to perform the lookup.  How can I get around this and save the ProductName?</p>
  
  <p>Here is the way the DropDownListFor reads:</p>
  
  <pre><code> @Html.DropDownListFor(model =&gt; model.ProductName, new SelectList(ViewBag.ProductData, "ProductId", "Name"), new { htmlAttributes = new { @id = "ProductName", @class = "ProductList" } });
  </code></pre>
  
  <p>The jQuery for the lookup is as follows:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
              $(document).ready(function () {
                  $(document).on("change", '[id*="ProductName"]', function () {
                      $.post("/QuoteViewModel/GetProduct", { pId: $(this).val() }, function (data) {
                          $("[id*='ProductId']").val(data.ProductId);
                          $("[id*='Price']").val(data.Price);
                      });
                  });
                });
              &lt;/script&gt;
  </code></pre>
  
  <p>and the controller action for the lookup is as follows:</p>
  
  <pre><code>[HttpPost]
          public ActionResult GetProduct(int pId)
          {
              var data = db.Products.Find(pId);
              return Json(data);
          }
  </code></pre>
  
  <p>Please note, the product name is called "Name" in the Product table, and "ProductName" in the QuoteDetail table.  Any help would be much appreciated.</p>
  
  <p>The code used for saving is as follows:</p>
  
  <pre><code>[HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Edit(QuoteViewModel qvm,[Bind(Include = "CustomerId,SalesRep,FirstName,LastName,Company,Address1,Address2,City,State,PostalCode,WorkPhone,CellPhone,Email,Discount,PaymentTerms")] Customer customer,
                                   [Bind(Include = "QuoteId,QuoteDetailId,ProductId,ProductName,Amount,ListPrice,Discount,Price")] List&lt;QuoteDetail&gt; quoteDetails,
                                   [Bind(Include = "QuoteId,CustomerId,Subtotal,Tax,Total,QuoteDate,GoodUntil,QuoteSent,DateApproved,DateOrdered")] Quote quote)
  
          {
  
              if (ModelState.IsValid)
              {
  
                  foreach (var quoteDetail in qvm.QuoteDetail)
                  {
  
                      QuoteDetail qd = db.QuoteDetails.FirstOrDefault(o =&gt; ((o.QuoteId == quoteDetail.QuoteId) &amp;&amp; (o.QuoteDetailId == quoteDetail.QuoteDetailId)));
  
                      if (qd != null)
                      {
                          qd.ProductId = quoteDetail.ProductId;
                          qd.ProductName = quoteDetail.ProductName;
                          qd.Amount = quoteDetail.Amount;
                          qd.ListPrice = quoteDetail.ListPrice;
                          qd.Discount = quoteDetail.Discount;
                          qd.Price = quoteDetail.Price;
                      }
                      else
                      {
                          db.QuoteDetails.Add(quoteDetail);
                      }
  
                  }
  
                  quote.QuoteId = qvm.QuoteId;
                  quote.GoodUntil = qvm.GoodUntil;
                  quote.Discount = qvm.Discount;
                  quote.DateApproved = qvm.DateApproved;
                  quote.DateOrdered = qvm.DateOrdered;
                  quote.Discount = qvm.Discount;
                  quote.QuoteDate = qvm.QuoteDate;
                  quote.QuoteSent = qvm.QuoteSent;
                  quote.Subtotal = qvm.Subtotal;
                  quote.Tax = qvm.Tax;
                  quote.Total = qvm.Total;
  
                  customer.CustomerId = qvm.CustomerId;
                  customer.Address1 = qvm.Address1;
                  customer.Address2 = qvm.Address2;
                  customer.CellPhone = qvm.CellPhone;
                  customer.City = qvm.City;
                  customer.Company = qvm.Company;
                  customer.Discount = qvm.Discount;
                  customer.Email = qvm.Email;
                  customer.FirstName = qvm.FirstName;
                  customer.LastName = qvm.LastName;
                  customer.PaymentTerms = qvm.PaymentTerms;
                  customer.PostalCode = qvm.PostalCode;
                  customer.SalesRep = qvm.SalesRep;
                  customer.State = qvm.State;
                  customer.WorkPhone = qvm.WorkPhone;
  
                  db.Entry(quote).State = EntityState.Modified;
                  db.Entry(customer).State = EntityState.Modified;
  
                  bool saveFailed;
                  do
                  {
                      saveFailed = false;
                      try
                      {
                          db.SaveChanges();
                      }
                      catch (DbUpdateConcurrencyException ex)
                      {
                          saveFailed = true;
                          var objContext = ((IObjectContextAdapter)db).ObjectContext;
                          // Get failed entry
                          var entry = ex.Entries.Single();
                          // Now call refresh on ObjectContext
                          objContext.Refresh(RefreshMode.ClientWins, entry.Entity);
  
  
                      }
  
                  } while (saveFailed);
  
                  return RedirectToAction("Index");
              }
              return View(qvm);
          }
      }
  }
  </code></pre>
  
  <p>Revised DropDownListFor:</p>
  
  <pre><code>@Html.DropDownListFor(model =&gt; model.ProductName, new SelectList(ViewBag.ProductData, "ProductId", "Name"), new { htmlAttributes = new { name = "ProductId", @id = "ProductName", @class = "ProductList" } });
  </code></pre>