public static ProductPoco ProductToPoco(ShopAroundEntities Dbcontext, PRODUCT product)
 {
     ProductPoco poco = new ProductPoco();
     poco.Product = product;
     poco.Brand = Dbcontext.BRANDS.Find(product.FK_Brands);
     poco.Category = Dbcontext.CATEGORIES.Find(product.Fk_Categories);
     poco.Type = Dbcontext.TYPES.Find(product.Fk_Type);
     poco.Vat = Dbcontext.VATs.Find(product.Fk_VAT);
     return poco;
 }
        public ActionResult Create(ProductPoco prod)
        {
            if (ModelState.IsValid)
                if (prod != null)
                {
                    var resultProduct = Dbcontext.setProduct(prod.Product.Reference, prod.Product.Availability,
                                                             prod.Product.Description, prod.Product.Price,
                                                             prod.Product.Name, prod.Product.Image,
                                                             prod.Product.Fk_VAT, prod.Product.FK_Brands,
                                                             prod.Product.Fk_Categories, prod.Product.Fk_Type).FirstOrDefault();
                    if (resultProduct != 0)
                        return RedirectToAction("Index");
                    else
                        ModelState.AddModelError("", "Erreur: Les informations sont incorrectes");
                }
            else
                ModelState.AddModelError("", "Erreur: Le modèle n'est pas valide");

            ViewBag.Brands = new SelectList(Dbcontext.BRANDS, "Id", "Name");
            ViewBag.Categories = new SelectList(Dbcontext.CATEGORIES, "Id", "Name");
            ViewBag.Types = new SelectList(Dbcontext.TYPES, "Id", "Name");
            ViewBag.Vats = new SelectList(Dbcontext.VATs, "Id", "Tax");
            return View();
        }
        public ActionResult Edit(int id)
        {
            PRODUCT product = Dbcontext.PRODUCTS.Find(id);
            ProductPoco poco = new ProductPoco();
            poco.Product= product;
            //Le FK_Adresse correspond à l'adresse principal
            poco.Brand = Dbcontext.BRANDS.Find(product.FK_Brands);
            poco.Category = Dbcontext.CATEGORIES.Find(product.Fk_Categories);
            poco.Type = Dbcontext.TYPES.Find(product.Fk_Type);
            poco.Vat = Dbcontext.VATs.Find(product.Fk_VAT);

            //Préparation de la vue
            ViewBag.Brands = new SelectList(Dbcontext.BRANDS, "Id", "Name");
            ViewBag.Categories = new SelectList(Dbcontext.CATEGORIES, "Id", "Name");
            ViewBag.Types = new SelectList(Dbcontext.TYPES, "Id", "Name");
            ViewBag.Vats = new SelectList(Dbcontext.VATs, "Id", "Tax");
            ViewBag.Id = id;

            return View(poco);
        }
 public ActionResult Edit(ProductPoco prod, string mySubmit)
 {
     if (mySubmit == "Modifier le produit")
     {
         if (ModelState.IsValid)
             if (prod != null)
             {
                 Dbcontext.Entry(prod.Product).State = EntityState.Modified;
                 Dbcontext.Entry(Dbcontext.BRANDS.Find(prod.Product.FK_Brands)).State = EntityState.Modified;
                 Dbcontext.Entry(Dbcontext.BRANDS.Find(prod.Product.Fk_Categories)).State = EntityState.Modified;
                 Dbcontext.Entry(Dbcontext.BRANDS.Find(prod.Product.Fk_Type)).State = EntityState.Modified;
                 Dbcontext.Entry(Dbcontext.BRANDS.Find(prod.Product.Fk_VAT)).State = EntityState.Modified;
                 if (Dbcontext.SaveChanges() > 0)
                     return RedirectToAction("Index");
                 else
                     ModelState.AddModelError("", "Erreur: Les informations sont incorrectes");
             }
             else
                 ModelState.AddModelError("", "Erreur: Le modèle n'est pas valide");
     }
     else if (mySubmit == "Supprimer le produit")
     {
         Dbcontext.Entry(prod.Product).State = EntityState.Deleted;
         Dbcontext.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.Brands = new SelectList(Dbcontext.BRANDS, "Id", "Name");
     ViewBag.Categories = new SelectList(Dbcontext.CATEGORIES, "Id", "Name");
     ViewBag.Types = new SelectList(Dbcontext.TYPES, "Id", "Name");
     ViewBag.Vats = new SelectList(Dbcontext.VATs, "Id", "Tax");
     return View(prod);
 }