Esempio n. 1
0
        public JsonResult Insert(OrderViewModel order)
        {
            bool status = false;
            if (ModelState.IsValid)
            {
                var orderHeader = new OrderHeader {
                    OrderDate = DateTime.Parse(order.DateTime),
                    Person = db.People.SingleOrDefault(x => x.Id == order.person.PersonId),
                    //PersonId = order.person.PersonId,
                    };

                //also need to save the orderdetails based on the products you choose
                foreach (var p in order.products)
                {
                    OrderDetail od = new OrderDetail
                    {
                        OrderHeader = orderHeader,
                        Product = db.Products.Single(x => x.Name == p.Name)
                    };
                    orderHeader.OrderDetails.Add(od);
                }

                db.OrderHeaders.Add(orderHeader);
                db.SaveChanges();
                status = true;
                return Json(new { success = status ,OrderId = orderHeader.OrderId});
            }
            return Json(new
            {
                success = false,
                errors = ModelState.Keys.SelectMany(i => ModelState[i].Errors).Select(m => m.ErrorMessage).ToArray()
            });
        }
Esempio n. 2
0
        public JsonResult Delete(OrderViewModel order)
        {
            bool status = false;
            if (order == null)
            {
                return Json(new
                {
                    success = status
                });
            }
            OrderHeader orderHeader = db.OrderHeaders.Find(order.OrderId);
            if (orderHeader == null)
            {
                return Json(new
                {
                    success = status
                });
            }

            //remove the orderdetails
            var ods = from o in db.OrderHeaders.Where(x => x.OrderId == order.OrderId)
                      from od in o.OrderDetails
                      select od;
            foreach (var item in ods)
            {
                db.OrderDetails.Remove(item);
            }
            //remove the order
            db.OrderHeaders.Remove(orderHeader);
            db.SaveChanges();
            status = true;
            return Json(new
            {
                success = status
            });
        }
Esempio n. 3
0
        public JsonResult Update(OrderViewModel order)
        {
            bool status = false;
            var orderHeader = db.OrderHeaders.Find(order.OrderId);
            orderHeader.OrderDate = DateTime.Parse(order.DateTime);
            orderHeader.Person = db.People.Find(order.person.PersonId);
            //orderHeader.PersonId = order.person.PersonId;


            //also need to save the orderdetails based on the products you choose
            foreach (var p in order.products)
            {
                OrderDetail od = new OrderDetail
                {
                    OrderHeader = orderHeader,
                    Product = db.Products.SingleOrDefault(x => x.Name == p.Name)
                };
                db.OrderDetails.Add(od);
                orderHeader.OrderDetails.Add(od);
            }

            //also need to save the orderdetails based on the products you choose
            if (ModelState.IsValid)
            {
                db.Entry(orderHeader).State = EntityState.Modified;
                db.SaveChanges();
                status = true;
                return Json(new { success = status });
            }
            return Json(new
            {
                success = false,
                errors = ModelState.Keys.SelectMany(i => ModelState[i].Errors).Select(m => m.ErrorMessage).ToArray()
            });
        }