public ActionResult Edit(ProductViewModel productVM)
        {
            if (productVM == null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            productVM.Clients = PopulateCliensList();//dropdown
            int?productID = productVM.ProductID;
            //passing dropdown selected items to the collection
            List <SelectListItem> selectedItems = productVM.Clients.Where(p => productVM.ClientIds.Contains(int.Parse(p.Value))).ToList();


            //dla nowego
            if (productID == 0)
            {
                Product newProduct = new Product();
                newProduct.Name        = productVM.Name;
                newProduct.Description = productVM.Description;
                newProduct.Price       = productVM.Price;
                _context.Products.Add(newProduct);
                _context.SaveChanges();

                if (selectedItems != null)
                {
                    newProduct.CPs = new List <ClientsProducts>();
                    foreach (var item in selectedItems)
                    {
                        var clientToAdd = new ClientsProducts {
                            ProductID = newProduct.ProductID, ClientID = int.Parse(item.Value)
                        };
                        newProduct.CPs.Add(clientToAdd);
                    }
                }
                _context.SaveChanges();
                return(RedirectToAction(nameof(Index)));
            }
            //dla edycji
            return(null);
        }
Beispiel #2
0
        public IActionResult CambiarStatusProducto([FromRoute] int id,
                                                   [FromRoute] int Status)
        {
            ClientsProducts ClientsProducts = _context.Clientes_Productos.Where(x => x.idClientes_Productos == id).FirstOrDefault();

            if (ClientsProducts == null)
            {
                return(NotFound());
            }
            ClientsProducts.status = Status;
            try
            {
                _context.SaveChanges();
                return(Ok());
            }
            catch (Exception)
            {
                return(BadRequest());

                throw;
            }
        }