public ActionResult Products_Update([DataSourceRequest] DataSourceRequest request, Product product)
        {
            //Manually add an model error in order to simulate validation error
            ModelState.AddModelError(/* Property name */ "Name", /* Validation message */ "My server error");

            //TODO: implement update functionality

            return Json(ModelState.ToDataSourceResult());
        }
 public IHttpActionResult Post(Product product)
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     db.Products.Add(product);
     //db.SaveChanges();
     return Created(product);
 }
        public static void RemoveProduct(this NorthwindEntities db, int id)
        {
            var entity = new Product();

            entity.ProductID = id;

            db.Products.Attach(entity);
            db.Products.Remove(entity);

            db.SaveChanges();
        }
        public ActionResult Update([DataSourceRequest]DataSourceRequest request, Product product)
        {
            if (product != null && ModelState.IsValid)
            {
                var target = products.First(p => p.ProductID == product.ProductID);
                target.ProductName = product.ProductName;
                target.Category = product.Category;

                //TODO: save the changes
            }

            return Json(new [] { product }.ToDataSourceResult(request));
        }
        public void Update(ProductViewModel product)
        {
            var entity = new Product();

            entity.ProductID = product.ProductID;
            entity.ProductName = product.ProductName;
            entity.UnitPrice = product.UnitPrice;
            entity.UnitsInStock = (short)product.UnitsInStock;
            entity.Discontinued = product.Discontinued;

            entities.Products.Attach(entity);
            entities.Entry(entity).State = EntityState.Modified;
            entities.SaveChanges();
        }
        public void Create(ProductViewModel product)
        {
            var entity = new Product();

            entity.ProductName = product.ProductName;
            entity.UnitPrice = product.UnitPrice;
            entity.UnitsInStock = (short)product.UnitsInStock;
            entity.Discontinued = product.Discontinued;

            entities.Products.Add(entity);
            entities.SaveChanges();

            product.ProductID = entity.ProductID;
        }
        public ActionResult Update([DataSourceRequest]DataSourceRequest request, Product product)
        {
            if (ModelState.IsValid)
            {
                var dataItem = Products.Where(p => p.ProductID == product.ProductID).FirstOrDefault();

                dataItem.ProductID = product.ProductID;
                dataItem.ProductName = product.ProductName;
                dataItem.UnitsInStock = product.UnitsInStock;
                dataItem.Available = product.Available;
            }

            // Return the updated entities as well as any validation errors.
            return Json(new [] { product }.ToDataSourceResult(request, ModelState));
        }
        public void Destroy(ProductViewModel product)
        {
            var entity = new Product();

            entity.ProductID = product.ProductID;

            entities.Products.Attach(entity);

            entities.Products.Remove(entity);

            var orderDetails = entities.Order_Details.Where(pd => pd.ProductID == entity.ProductID);

            foreach (var orderDetail in orderDetails)
            {
                entities.Order_Details.Remove(orderDetail);
            }

            entities.SaveChanges();
        }
        // PUT: odata/Products(5)
        public IHttpActionResult Put([FromODataUri] int key, Product product)
        {   
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (key != product.ProductID)
            {
                return BadRequest();
            }

            db.Products.Attach(product);
            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(key))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return Updated(product);
        }