// To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            Cart.Location     = _context.Locations.SingleOrDefault(l => l.Id == SelectedLocationId);
            Cart.StokPosition = _context.StokPositions.SingleOrDefault(sp => sp.Id == SelectedStokPositionId);

            var date = DateTimeOffset.Now;
            var rows = new List <Model.Row>();

            int i = 0;

            foreach (var id in SelectedProductIds)
            {
                if (SelectedChecks[i] == "1")
                {
                    rows.Add(new Row
                    {
                        Cart             = Cart,
                        CreatedDate      = date,
                        LastModifiedDate = date,
                        Product          = _context.Products.SingleOrDefault(product => product.Id == id),
                        Quantity         = SelectedQuatities[i]
                    });
                }
                i++;
            }
            Cart.CreatedDate      = date;
            Cart.LastModifiedDate = date;
            Cart.Rows             = rows;

            _context.Carts.Add(Cart);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Products.Add(Product);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Beispiel #3
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Product = await _context.Products.FindAsync(id);

            if (Product != null)
            {
                _context.Products.Remove(Product);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
        // To protect from overposting attacks, enable the specific properties you want to bind to, for
        // more details, see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            _context.Attach(Cart).State = EntityState.Modified;

            try
            {
                Cart = await _context.Carts.Include(x => x.Rows).FirstOrDefaultAsync(m => m.Id == Cart.Id);

                Cart.Location     = _context.Locations.SingleOrDefault(l => l.Id == SelectedLocationId);
                Cart.StokPosition = _context.StokPositions.SingleOrDefault(sp => sp.Id == SelectedStokPositionId);
                int i    = 0;
                var date = DateTimeOffset.Now;

                foreach (var product in Products)
                {
                    if (SelectedChecks[i] == "1")
                    {
                        if (Cart.Rows.Any(r => r.Product.Code == product.Code))
                        {
                            var row = Cart.Rows.SingleOrDefault(r => r.Product.Code == product.Code);
                            row.Cart             = Cart;
                            row.Quantity         = SelectedQuatities[i];
                            row.LastModifiedDate = date;
                        }
                        else
                        {
                            Cart.Rows.Add(new Row
                            {
                                Cart             = Cart,
                                CreatedDate      = date,
                                LastModifiedDate = date,
                                Product          = _context.Products.SingleOrDefault(p => p.Code == product.Code),
                                Quantity         = SelectedQuatities[i]
                            });
                        }
                    }
                    else
                    {
                        if (Cart.Rows.Any(r => r.Product.Code == product.Code))
                        {
                            var toDell = Cart.Rows.SingleOrDefault(x => x.Product.Id == product.Id && x.Cart.Id == Cart.Id);
                            Cart.Rows.Remove(toDell);
                            _context.Rows.Remove(toDell);
                        }
                        else
                        {
                        }
                    }
                    i++;
                }

                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CartExists(Cart.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }