public async Task <IActionResult> Create([Bind("IdStock,FkProduct,Amount,FkStorehouse,Status")] Stocks stocks)
        {
            ViewBag.COOKIE_LOGGED_USERNAME = Request.Cookies["COOKIE_LOGGED_USERNAME"];

            bool alreadyExists = _context.Stocks.Where(s => s.FkProduct == stocks.FkProduct && s.FkStorehouse == stocks.FkStorehouse).Count() > 0;

            if (alreadyExists)
            {
                ViewBag.Message = "Cannot create! Such stock already exists!";
            }

            if (!ModelState.IsValid || alreadyExists)
            {
                ViewData["FkProduct"]    = new SelectList(_context.Products, "IdProduct", "Name", stocks.FkProduct);
                ViewData["FkStorehouse"] = new SelectList(_context.Storehouses, "IdStorehouse", "IdStorehouse", stocks.FkStorehouse);
                return(View(stocks));
            }

            try
            {
                stocks.IdStock = _context.Stocks.Max(s => s.IdStock) + 1;
            }
            catch (Exception e)
            {
                stocks.IdStock = 1;
            }
            _context.Add(stocks);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Create([Bind("IdUnit,Name,ShortName,Status")] Units units)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    units.IdUnit = _context.Units.Max(unit => unit.IdUnit) + 1;
                }
                catch (Exception e)
                {
                    units.IdUnit = 1;
                }
                _context.Add(units);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(units));
        }
示例#3
0
        public async Task <IActionResult> Create([Bind("IdProduct,Name,BasePricePerUnit,ImgFile,PdfFile,FkUnit")] Products products)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    products.IdProduct = _context.Products.Max(product => product.IdProduct) + 1;
                }
                catch (Exception e)
                {
                    products.IdProduct = 1;
                }
                _context.Add(products);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["FkUnit"] = new SelectList(_context.Units, "IdUnit", "Name", products.FkUnit);
            return(View(products));
        }