예제 #1
0
        public ActionResult Create(Purchase purchase)
        {
            try
            {
                if (ModelState.IsValid && !servicePurchase.ValidatePurchase(purchase.Products_ID, purchase.Buyers_ID))
                {
                    servicePurchase.AddPurchase(purchase.Products_ID, purchase.Buyers_ID, purchase.Amount);
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Something wrong");
            }

            ViewBag.Buyers_ID = new SelectList(serviceBuyers.GetBuyers(), "ID", "FirstName", purchase.Buyers_ID);
            ViewBag.Products_ID = new SelectList(serviceProducts.GetProducts(), "ID", "NameProduct", purchase.Products_ID);
            return View(purchase);
        }
예제 #2
0
        public ActionResult Edit(Purchase purchase, FormCollection form)
        {
            try
            {
                int buyerID = Convert.ToInt32(form["buyerID"]);
                int oldProduct = Convert.ToInt32(form["oldProduct"]);
                if (ModelState.IsValid)
                {
                    if (servicePurchase.ValidatePurchase(purchase.Products_ID, buyerID) && oldProduct != purchase.Products_ID)
                    {
                        ModelState.AddModelError("", "That order already exists.");

                        ViewBag.ProductsID = new SelectList(serviceProducts.GetProducts(), "ID", "NameProduct", purchase.Products_ID);
                        purchase.Buyers = serviceBuyers.GetBuyerByID(buyerID);
                        return View(purchase);
                    }

                    servicePurchase.UpdatePurchase(oldProduct, purchase.Products_ID, purchase.Buyers_ID, purchase.Amount);
                    return RedirectToAction("Index");
                }
                purchase.Buyers = serviceBuyers.GetBuyerByID(buyerID);
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Something wrong");
            }

            ViewBag.Products_ID = new SelectList(serviceProducts.GetProducts(), "ID", "NameProduct", purchase.Products_ID);
            return View(purchase);
        }