コード例 #1
0
        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());
        }
コード例 #2
0
 public IHttpActionResult Post(Product product)
 {
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     db.Products.Add(product);
     //db.SaveChanges();
     return Created(product);
 }
コード例 #3
0
        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();
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        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();
        }
コード例 #6
0
        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;
        }
コード例 #7
0
        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));
        }
コード例 #8
0
        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();
        }
コード例 #9
0
        // 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);
        }