Ejemplo n.º 1
0
        // GET: Orders
        public ActionResult NewOrder()
        {
            var orderView = new OrderView();
            orderView.Customer = new Customer();
            orderView.ProductOrders = new List<ProductOrder>();

            //Utilizo session para guardar los datos, en el viwbag no los puedo guuardar porque es efimero,
            // solo muestra los datos y no los gretiene o guarda:
            Session["orderView"] = orderView;//Almaceno los datos: 

            //para mandar la lista a los cientes:
            var list = db.Customers.ToList();
            list.Add(new Customer { CustomerId = 0, FirstName = "[Seleccione un Cliente.....]"});
            ViewBag.CustomerId = new SelectList(list.OrderBy(c => c.LastName), "CustomerId", "FullName");
          

            return View(orderView);
        }
Ejemplo n.º 2
0
        public ActionResult NewOrder(OrderView orderView)
        {
            //Recupero o leo los datos(objetos) del la session:
             orderView = Session["orderView"] as OrderView;//hago un casting
             
            var customerId = int.Parse(Request["CustomerId"]);

            if (customerId == 0)
            {
                //para mandar la lista a los cientes:
                var list1 = db.Customers.ToList();
                list1.Add(new Customer { CustomerId = 0, FirstName = "[Seleccione un Cliente.....]" });
                ViewBag.CustomerId = new SelectList(list1.OrderBy(c => c.LastName), "CustomerId", "FullName");  
                 ViewBag.Error = "Debe Seleccionar un Cliente.....";
                
                return View(orderView);
            }

            var customer = db.Customers.Find(customerId);
            if (customer == null)
            {
                //para mandar la lista a los cientes:
                var list1 = db.Customers.ToList();
                list1.Add(new Customer { CustomerId = 0, FirstName = "[Seleccione un Cliente.....]" });
                ViewBag.CustomerId = new SelectList(list1.OrderBy(c => c.LastName), "CustomerId", "FullName");

                ViewBag.Error = "El Cliente no Existe.....";
                
                return View(orderView);
            }
            //validando detalles del producto: lo tienen que seleccionar
            if (orderView.ProductOrders.Count == 0)
            {
                //para mandar la lista a los cientes:
                var list1 = db.Customers.ToList();
                list1.Add(new Customer { CustomerId = 0, FirstName = "[Seleccione un Cliente.....]" });
                ViewBag.CustomerId = new SelectList(list1.OrderBy(c => c.LastName), "CustomerId", "FullName");

                ViewBag.Error = "Debe Ingresar Detalle del Producto.....";

                return View(orderView);
            }

            var orderId = 0;
            //Manejo transacsional:
            //Start Transction:
            //Inyectar error para prueba de la transaction:
            int i = 0;
            using (var transaction = db.Database.BeginTransaction())
            {
                try
                {
                    var order = new Order
                    {
                        CustomerId = customerId,
                        OrderDate = DateTime.Now,
                        OrderStatus = OrderStatus.Created
                    };

                    db.Orders.Add(order);
                    db.SaveChanges();

                    //onsulta linq para obtner el maximo de la tabla orders:
                    orderId = db.Orders.ToList().Select(o => o.OrderId).Max();

                    foreach (var item in orderView.ProductOrders)
                    {
                        var orderDetail = new OrderDetail
                        {
                            ProductId = item.ProductId,
                            Descripcion = item.Descripcion,
                            Price = item.Price,
                            Quantity = item.Quantity,
                            OrderId = orderId
                        };

                        db.OrderDetails.Add(orderDetail);
                        db.SaveChanges();

                        //Error provocado para probar transaction
                        //i++;
                        //if (i > 2)
                        //{
                        //    int a = 0;
                        //    i /= a;

                        //}

                    }

                    //Confirmo la transaction;
                    transaction.Commit();

                }
                catch (Exception ex)
                {

                    transaction.Rollback();
                    ViewBag.Error = "ERROR: " + ex.Message;
                  

                    //para mandar la lista a los cientes:
                    var listC = db.Customers.ToList();
                    listC.Add(new Customer { CustomerId = 0, FirstName = "[Seleccione un Cliente.....]" });
                    ViewBag.CustomerId = new SelectList(listC.OrderBy(c => c.LastName), "CustomerId", "FullName");

                    return View(orderView);
                }     

           
          }//end Transaction

            ViewBag.Message = string.Format("La orden: {0}, Grabada OK.", orderId);

            //para mandar la lista a los cientes:
            var list = db.Customers.ToList();
            list.Add(new Customer { CustomerId = 0, FirstName = "[Seleccione un Cliente.....]" });
            ViewBag.CustomerId = new SelectList(list.OrderBy(c => c.LastName), "CustomerId", "FullName");

            orderView = new OrderView();
            orderView.Customer = new Customer();
            orderView.ProductOrders = new List<ProductOrder>();
            Session["orderView"] = orderView;

            return View(orderView);
        }