title: Asp.Net MVC 4 Controller to update database
body: <p>I have the following code:</p>
  
  <p>Model</p>
  
  <pre><code>public class Orders
  {
      public int Id { get; set; }
      public int UserId { get; set; }
      public int ProductId { get; set; }
      public int Quantity { get; set; }
      public double Price { get; set; }
      public bool OrderStatus { get; set; }
      public string OrderNumber { get; set; }
      public string AdminStatus { get; set; }
      public bool PaymentStatus { get; set; }
      public DateTime OrderDate { get; set; }
  }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code>public ActionResult CheckOut()
          {
              if (Request.IsAjaxRequest())
              {
                  var userid = WebSecurity.CurrentUserId;
                  var data = _bbdb.Orders.Where(x =&gt; x.OrderStatus == false &amp;&amp; x.UserId == userid).ToList();
  
                  foreach (var item in data)
                  {
                      _bbdb.Orders.Add(new Orders
                      {
                          Id = item.Id,
                          UserId = item.UserId,
                          ProductId = item.ProductId,
                          Quantity = item.Quantity,
                          Price = item.Price,
                          OrderStatus = true,
                          OrderNumber = item.UserId + DateTime.Now.ToShortTimeString(),
                          AdminStatus = item.AdminStatus,
                          PaymentStatus = item.PaymentStatus,
                          OrderDate = DateTime.Today
                      });
  
                      _bbdb.SaveChanges();
  
                  }
              }
  
              var cartdata = GetCartItems();
              return PartialView("_CartTable", cartdata);
          }
  </code></pre>
  
  <p>How can I change this bit code  <code>_bbdb.Orders.Add(new Orders</code> to UPDATE instead ADD.</p>
  
  <p>I thought something like <code>_bbdb.Orders.AddOrUpdate(new Orders</code> would do the trick, but I does not exist.</p>
  
  <p>It is adding the right value, but I want to update the records not add new ones.</p>
  
  <p>Any suggestion on how to improve or change will be appreciated.</p>