public ActionResult AddEditPrePO(Int32?PrePurcherseOrderId, Int32?FatherId)
        {
            var model = new AddEditPrePOViewModel();

            model.Fill(CargarDatosContext(), PrePurcherseOrderId, FatherId);
            return(View(model));
        }
        public ActionResult AddEditPrePO(AddEditPrePOViewModel model, FormCollection frm)
        {
            try
            {
                String prePOCode = String.Empty;

                using (var transaction = new TransactionScope())
                {
                    PrePurcherseOrder prePO = null;

                    if (model.PrePurcherseOrderId.HasValue)
                    {
                        prePO = context.PrePurcherseOrder.Include(x => x.PrePurcherseOrderDetail).FirstOrDefault(x => x.PrePurcherseOrderId == model.PrePurcherseOrderId);

                        var lstPrePurcherseOrderDetail = prePO.PrePurcherseOrderDetail.ToList();
                        foreach (var d in lstPrePurcherseOrderDetail)
                        {
                            context.PrePurcherseOrderDetail.Remove(d);
                        }
                    }
                    else
                    {
                        prePO       = new PrePurcherseOrder();
                        prePO.State = ConstantHelpers.ESTADO.ACTIVO;
                        context.PrePurcherseOrder.Add(prePO);
                    }

                    prePO.RegistrationDate = model.Registration.ToDateTime();
                    prePO.SupplierId       = model.SupplierId;
                    prePO.CountryId        = model.CountryId;
                    prePO.ShipmentDate     = model.ShipmentDate.ToDateTime();
                    prePO.Text             = model.Text;
                    prePO.SendSupply       = model.SendSupply;
                    prePO.Code             = model.Code;
                    prePO.DateOrder        = model.DateOrder.ToDateTime();


                    var lstQuantity = frm.AllKeys.Where(x => x.StartsWith("quantity-")).ToList();
                    foreach (var quantity in lstQuantity)
                    {
                        PrePurcherseOrderDetail detail = new PrePurcherseOrderDetail();

                        var productId   = quantity.Replace("quantity-", String.Empty).ToInteger();
                        var intQuantity = frm[quantity].ToInteger();
                        var measureunit = frm["measureunit-" + productId].ToInteger();

                        detail.ProductId         = productId;
                        detail.Quantity          = intQuantity;
                        detail.MeasureUnitId     = measureunit;
                        detail.State             = ConstantHelpers.ESTADO.ACTIVO;
                        detail.PrePurcherseOrder = prePO;

                        context.PrePurcherseOrderDetail.Add(detail);
                    }

                    context.SaveChanges();

                    if (String.IsNullOrEmpty(model.Code))
                    {
                        prePO.Code = prePO.PrePurcherseOrderId.ToString();
                        prePOCode  = prePO.Code;
                        context.SaveChanges();
                    }

                    transaction.Complete();
                }

                PostMessage(MessageType.Success, "Los datos se guardaron exitosamente. Pre Purcharse Order: " + prePOCode);
                return(RedirectToAction("ListPrePO", new { FatherId = model.FatherId }));
            }
            catch (Exception ex)
            {
                PostMessage(MessageType.Error);
                model.Fill(CargarDatosContext(), model.PrePurcherseOrderId, model.FatherId);
                return(View(model));
            }
        }