public ActionResult Create([Bind(Include = "Id,Name,Image,Description,CompanyID")] Ecolabel ecolabel,
                                   HttpPostedFileBase file, List <int> EcoTypeID, List <int> CountryID, List <int> DemandID)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    ecolabel.Image = new byte[file.ContentLength];
                    file.InputStream.Read(ecolabel.Image, 0, file.ContentLength);
                }
                AddCountry(CountryID, ecolabel);
                AddDemand(DemandID, ecolabel);
                AddType(EcoTypeID, ecolabel);

                db.Ecolabels.Add(ecolabel);
                db.SaveChanges();
                return(RedirectToAction("IndexAdmin"));
            }

            ViewBag.CompanyID = new SelectList(db.Companies, "Id", "Name");
            ViewBag.EcoTypeID = new SelectList(db.EcoTypes, "Id", "Name");
            ViewBag.CountryID = new SelectList(db.Countries, "Id", "Name");
            ViewBag.DemandID  = new SelectList(db.Demands, "Id", "Rule");
            return(View(ecolabel));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            Ecolabel ecolabel = db.Ecolabels.Find(id);

            db.Ecolabels.Remove(ecolabel);
            db.SaveChanges();
            return(RedirectToAction("IndexAdmin"));
        }
        public ActionResult ViewPhoto(int?id)
        {
            Ecolabel ecolabel = db.Ecolabels.Find(id);

            if (ecolabel == null)
            {
                return(HttpNotFound());
            }
            return(File(ecolabel.Image, "image/png"));
        }
        // GET: Ecolabels/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Ecolabel ecolabel = db.Ecolabels.Find(id);

            if (ecolabel == null)
            {
                return(HttpNotFound());
            }
            return(View(ecolabel));
        }
        public ActionResult Edit([Bind(Include = "Id,Name,Image,Description,CompanyID")] Ecolabel ecolabel, HttpPostedFileBase file,
                                 List <int> EcoTypeID, List <int> CountryID, List <int> DemandID)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    db.Entry(ecolabel).State = EntityState.Modified;
                    ecolabel.Image           = new byte[file.ContentLength];
                    file.InputStream.Read(ecolabel.Image, 0, file.ContentLength);
                }

                List <int> delete = new List <int>();
                delete = (from ce in db.Country_Ecolabel
                          where ce.EcolabelID == ecolabel.Id
                          select ce.Id).ToList();
                foreach (int elem in delete)
                {
                    db.Country_Ecolabel.Remove(db.Country_Ecolabel.Find(elem));
                }

                delete = (from de in db.Demand_Ecolabel
                          where de.EcolabelID == ecolabel.Id
                          select de.Id).ToList();
                foreach (int elem in delete)
                {
                    db.Demand_Ecolabel.Remove(db.Demand_Ecolabel.Find(elem));
                }

                delete = (from ee in db.EcoType_Ecolabel
                          where ee.EcolabelID == ecolabel.Id
                          select ee.Id).ToList();
                foreach (int elem in delete)
                {
                    db.EcoType_Ecolabel.Remove(db.EcoType_Ecolabel.Find(elem));
                }

                AddCountry(CountryID, ecolabel);
                AddDemand(DemandID, ecolabel);
                AddType(EcoTypeID, ecolabel);

                db.SaveChanges();
                return(RedirectToAction("IndexAdmin"));
            }
            ViewBag.CompanyID = new SelectList(db.Companies, "Id", "Name");
            ViewBag.EcoTypeID = new SelectList(db.EcoTypes, "Id", "Name");
            ViewBag.CountryID = new SelectList(db.Countries, "Id", "Name");
            ViewBag.DemandID  = new SelectList(db.Demands, "Id", "Rule");
            return(View(ecolabel));
        }
 public void AddDemand(List <int> DemandsList, Ecolabel ecolabel)
 {
     if (DemandsList != null)
     {
         int i = 0;
         foreach (int elem in DemandsList)
         {
             Demand_Ecolabel de = new Demand_Ecolabel();
             de.EcolabelID = ecolabel.Id;
             de.DemandID   = DemandsList[i];
             db.Demand_Ecolabel.Add(de);
             i++;
         }
     }
 }
 public void AddCountry(List <int> CountriesList, Ecolabel ecolabel)
 {
     if (CountriesList != null)
     {
         int i = 0;
         foreach (int elem in CountriesList)
         {
             Country_Ecolabel ce = new Country_Ecolabel();
             ce.EcolabelID = ecolabel.Id;
             ce.CountryID  = CountriesList[i];
             db.Country_Ecolabel.Add(ce);
             i++;
         }
     }
 }
 public void AddType(List <int> TypesList, Ecolabel ecolabel)
 {
     if (TypesList != null)
     {
         int i = 0;
         foreach (int item in TypesList)
         {
             EcoType_Ecolabel ee = new EcoType_Ecolabel();
             ee.EcolabelID = ecolabel.Id;
             ee.EcoTypeID  = TypesList[i];
             db.EcoType_Ecolabel.Add(ee);
             i++;
         }
     }
 }
        public ActionResult AddConnectionCountryEcolabel(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Ecolabel ecolabel = db.Ecolabels.Find(id);

            if (ecolabel == null)
            {
                return(HttpNotFound());
            }
            ViewBag.EcolabelID = ecolabel.Name;
            return(RedirectToAction("Create", "Country_Ecolabel", ViewBag.EcolabelName = ecolabel.Name));

            return(View(ecolabel));
        }
        //public ActionResult Create([Bind(Include = "Id,Name,Image,Description,CompanyID")] Ecolabel ecolabel)
        //{
        //    if (ModelState.IsValid)
        //    {

        //        db.Ecolabels.Add(ecolabel);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }

        //    ViewBag.CompanyID = new SelectList(db.Companies, "Id", "Name", ecolabel.CompanyID);
        //    return View(ecolabel);
        //}

        // GET: Ecolabels/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Ecolabel ecolabel = db.Ecolabels.Find(id);

            if (ecolabel == null)
            {
                return(HttpNotFound());
            }
            ViewBag.CompanyID = new SelectList(db.Companies, "Id", "Name");
            ViewBag.EcoTypeID = new SelectList(db.EcoTypes, "Id", "Name");
            ViewBag.CountryID = new SelectList(db.Countries, "Id", "Name");
            ViewBag.DemandID  = new SelectList(db.Demands, "Id", "Rule");

            return(View(ecolabel));
        }