public async Task <IActionResult> Create([Bind("ID,Name")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(product));
        }
Example #2
0
        public async Task <IActionResult> AddProduct(DashboardModel model)
        {
            if (ModelState.IsValid)
            {
                var check = _ccontext.products.SingleOrDefault(p => p.name == model.product.name);


                using (var stream = new MemoryStream())
                {
                    await model.images.CopyToAsync(stream);

                    model.product.images = stream.ToArray();
                }

                //check if product exist, if yes, just update it
                if (check == null)
                {
                    Product product = model.product;
                    _ccontext.Add(product);
                    _ccontext.SaveChanges();
                }
                else
                {
                    check.quantity   = model.product.quantity;
                    check.price      = model.product.price;
                    check.updated_at = DateTime.Now;
                    check.images     = model.product.images;
                    // check.image = model.product.image;
                    _ccontext.SaveChanges();
                }

                return(RedirectToAction("Products"));
            }
            List <Product> products = _ccontext.products.ToList();
            DashboardModel newmodel = new DashboardModel()
            {
                products = products,
            };

            return(View("Products", newmodel));
        }