// 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(List <OrderItem> orderItems)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Order.Items = await _context.OrderItem.Where(item => item.OrderId == Order.Id).ToListAsync();

            _context.Attach(Order).State = EntityState.Modified;

            Order.Items = orderItems;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderExists(Order.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            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.Attach(DietTag).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!DietTagExists(DietTag.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Esempio n. 3
0
        // 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(int[] dietselection)
        {
            //CHANGE: Removed Request.Form["dietselection"] and replaced with parameter in handler as seen above. (also parses string values to ints)
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            //Make sure the many-to-many is loaded into the model.
            Product.ProductTags = await _context.ProductTags.Where(pt => pt.ProductId == Product.Id).ToListAsync();

            _context.Attach(Product).State = EntityState.Modified;

            //var dietTagsIds = Request.Form["dietselection"];

            var dietTags = _context.DietTag.Where(tag => dietselection.Any(id => id == tag.Id));
            var newTags  = new List <ProductTag>();

            foreach (var dietTag in dietTags)
            {
                var productTag = new ProductTag
                {
                    Product   = Product,
                    ProductId = Product.Id,
                    DietTag   = dietTag,
                    DietTagId = dietTag.Id
                };
                newTags.Add(productTag);
            }
            //Update many-to-many trough the product.
            //This way there is no need to check if a productTag already exists.
            Product.ProductTags = newTags;


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(Product.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

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