public ActionResult PostProduct(Product product) { if (!ModelState.IsValid) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var id = 1; var lastItem = ProductDataSource.LatestProducts.LastOrDefault(); if (lastItem != null) { id = lastItem.Id + 1; } product.Id = id; ProductDataSource.LatestProducts.Add(product); // گرید آی دی جدید را به این صورت دریافت میکند return Json(new DataSourceResult { Data = new[] { product } }); }
[HttpPut] // Add it to fix this error: The requested resource does not support http method 'PUT' public ActionResult UpdateProduct(int id, Product product) { var item = ProductDataSource.LatestProducts .Select( (prod, index) => new { Item = prod, Index = index }) .FirstOrDefault(x => x.Item.Id == id); if (item == null) return new HttpNotFoundResult(); if (!ModelState.IsValid || id != product.Id) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); ProductDataSource.LatestProducts[item.Index] = product; //Return HttpStatusCode.OK return new HttpStatusCodeResult(HttpStatusCode.OK); }