public ActionResult OrderDetails_Update([DataSourceRequest]DataSourceRequest request, OrderDetailViewModel order)
        {
            if (ModelState.IsValid)
            {
                using (var northwind = new NorthwindEntities())
                {

                    var entity = northwind.Order_Details.FirstOrDefault(detail => detail.OrderID == order.OrderID && detail.ProductID == order.ProductID);
                    if (entity == null)
                    {
                        string errorMessage = string.Format("Cannot update record with ProductID:{0} and OrderID:{1} as it's not available in current order.", order.OrderID, order.ProductID);
                        ModelState.AddModelError("", errorMessage);
                    }
                    else
                    {
                        entity.UnitPrice = order.UnitPrice;
                        entity.Quantity = (short)order.Quantity;
                        entity.Discount = order.Discount;

                        northwind.SaveChanges();
                    }
                }
            }
            return Json(new[] { order }.ToDataSourceResult(request, ModelState));
        }
 public ActionResult OrderDetails_Create([DataSourceRequest]DataSourceRequest request, OrderDetailViewModel order, int ParentID)
 {
     if (ModelState.IsValid)
     {
         using (var northwind = new NorthwindEntities())
         {
             var existingEntity = northwind.Order_Details.FirstOrDefault(detail => detail.OrderID == ParentID && detail.ProductID == order.ProductID);
             if (existingEntity != null)
             {
                 string errorMessage = string.Format("Record with ProductID:{0} and OrderID:{1} already exists in current order.", order.OrderID, order.ProductID);
                 ModelState.AddModelError("", errorMessage);
             }
             else
             {
                 var entity = new Order_Detail
                 {
                     OrderID = ParentID,
                     ProductID = order.ProductID,
                     UnitPrice = order.UnitPrice,
                     Quantity = (short)order.Quantity,
                     Discount = order.Discount
                 };
                 northwind.Order_Details.Add(entity);
                 northwind.SaveChanges();
                 order.OrderID = entity.OrderID;
             }
         }
     }
     return Json(new[] { order }.ToDataSourceResult(request, ModelState));
 }