예제 #1
0
        public ActionResult Create([Bind(Include = "ID,Place,Amount,NumberOfProducts,UOM,CreatedAt")] Purchase purchase)
        {
            if (ModelState.IsValid)
            {
                foreach (string upload in Request.Files)
                {
                    if (Request.Files[upload].ContentLength == 0)
                    {
                        continue;
                    }
                    string pathToSave = Server.MapPath(GlobalConstants.PATH_UPLOADED_IMAGES);
                    if (!Directory.Exists(pathToSave))
                    {
                        Directory.CreateDirectory(pathToSave);
                    }
                    string   filename  = Path.GetFileName(Request.Files[upload].FileName);
                    string[] extension = filename.Split('.');

                    string serverFileName = FileUploader.GenerateUniqName() + "." + extension[extension.Length - 1];

                    Request.Files[upload].SaveAs(Path.Combine(pathToSave, serverFileName));
                    purchase.FileName         = serverFileName;
                    purchase.FileOriginalName = filename;
                }

                var formData              = Request.Form;
                var purchaseCategoryId    = Int32.Parse(formData.Get("PurchaseCategory"));
                var purchaseEventCategory = Int32.Parse(formData.Get("PurchaseEventCategory"));

                purchase.PurchaseCategory      = db.PurchaseCategories.Find(purchaseCategoryId);
                purchase.PurchaseEventCategory = db.PurchaseEventCategories.Find(purchaseEventCategory);

                db.Purchases.Add(purchase);
                db.SaveChanges();
                return(RedirectToAction("Details", "Purchases", new { id = purchase.ID }));
            }

            return(View(purchase));
        }
예제 #2
0
        public ActionResult Create([Bind(Include = "ID,Name,Quantity,ProductType,Cost,CostPerUnit,ForWho,IsMinimalNecesarry,PurchaseIdReceiver")] Product product)
        {
            if (ModelState.IsValid)
            {
                foreach (string upload in Request.Files)
                {
                    if (Request.Files[upload].ContentLength == 0)
                    {
                        continue;
                    }
                    string pathToSave = Server.MapPath(GlobalConstants.PATH_UPLOADED_IMAGES);
                    if (!Directory.Exists(pathToSave))
                    {
                        Directory.CreateDirectory(pathToSave);
                    }
                    string   filename  = Path.GetFileName(Request.Files[upload].FileName);
                    string[] extension = filename.Split('.');

                    string serverFileName = FileUploader.GenerateUniqName() + "." + extension[extension.Length - 1];

                    Request.Files[upload].SaveAs(Path.Combine(pathToSave, serverFileName));
                    product.FileName         = serverFileName;
                    product.FileOriginalName = filename;
                }

                var purchase = db.Purchases.Find(product.PurchaseIdReceiver);
                //roduct.Purchase = purchase.CastToSelf();
                product.Purchase = purchase;

                db.Products.Add(product);
                db.SaveChanges();
                return(PartialView("ApiProductIndex", product));
                //return RedirectToAction("Index");
            }

            return(View("ApiProductIndex", product));
        }