コード例 #1
0
        public ActionResult Orders_Update([DataSourceRequest]DataSourceRequest request, OrderViewModel order)
        {
            if (ModelState.IsValid)
            {
                using (var northwind = new NorthwindEntities())
                {
                    var entity = northwind.Orders.FirstOrDefault(o => o.OrderID == order.OrderID);
                    if (entity == null)
                    {
                        string errorMessage = string.Format("Cannot update record with OrderID:{0} as it's not available.", order.OrderID);
                        ModelState.AddModelError("", errorMessage);
                    }
                    else
                    {
                        entity.CustomerID = order.CustomerID;
                        entity.EmployeeID = order.EmployeeID;
                        entity.OrderDate = order.OrderDate;
                        entity.ShipCountry = order.ShipCountry;
                        entity.ShipVia = order.ShipVia;
                        entity.ShippedDate = order.ShippedDate;
                        entity.ShipName = order.ShipName;
                        entity.ShipAddress = order.ShipAddress;
                        entity.ShipCity = order.ShipCity;
                        entity.ShipPostalCode = order.ShipPostalCode;

                        northwind.Orders.Attach(entity);
                        northwind.Entry(entity).State = EntityState.Modified;
                        northwind.SaveChanges();
                    }
                }
            }
            return Json(new[] { order }.ToDataSourceResult(request, ModelState));
        }
コード例 #2
0
        public ActionResult Orders_Destroy([DataSourceRequest]DataSourceRequest request, OrderViewModel order)
        {
            if (ModelState.IsValid)
            {
                using (var northwind = new NorthwindEntities())
                {
                    List<Order_Detail> details = northwind.Order_Details.Where(od => od.OrderID == order.OrderID).ToList();

                    foreach (var orderDetail in details)
                    {
                        northwind.Order_Details.Remove(orderDetail);
                    }

                    var entity = new Order
                    {
                        CustomerID = order.CustomerID,
                        OrderID = order.OrderID,
                        EmployeeID = order.EmployeeID,
                        OrderDate = order.OrderDate,
                        ShipCountry = order.ShipCountry,
                        ShipVia = order.ShipVia,
                        ShippedDate = order.ShippedDate,
                        ShipName = order.ShipName,
                        ShipAddress = order.ShipAddress,
                        ShipCity = order.ShipCity,
                        ShipPostalCode = order.ShipPostalCode
                    };
                    northwind.Orders.Attach(entity);
                    northwind.Orders.Remove(entity);
                    northwind.SaveChanges();
                }
            }
            return Json(new[] { order }.ToDataSourceResult(request, ModelState));
        }
コード例 #3
0
 public ActionResult Orders_Create([DataSourceRequest]DataSourceRequest request, OrderViewModel order)
 {
     if (ModelState.IsValid)
     {
         using (var northwind = new NorthwindEntities())
         {
             var entity = new Order
             {
                 CustomerID = order.CustomerID,
                 EmployeeID = order.EmployeeID,
                 OrderDate = order.OrderDate,
                 ShipCountry = order.ShipCountry,
                 ShipVia = order.ShipVia,
                 ShippedDate = order.ShippedDate,
                 ShipName = order.ShipName,
                 ShipAddress = order.ShipAddress,
                 ShipCity = order.ShipCity,
                 ShipPostalCode = order.ShipPostalCode
             };
             northwind.Orders.Add(entity);
             northwind.SaveChanges();
             order.OrderID = entity.OrderID;
         }
     }
     return Json(new[] { order }.ToDataSourceResult(request, ModelState));
 }