public ActionResult Create([Bind(Include = "IdProduit,Nom,Couleur,Description,Prix,IdSousCategorie,DateMiseEnVente,Promotion,CodePays,Stock")] Produits produits, HttpPostedFileBase[] file) { if (ModelState.IsValid) { db.Produits.Add(produits); db.SaveChanges(); if (!(file.Count() == 0)) { for (int i = 0; i < file.Count(); i++) { var fileName = Path.GetFileName(file[i].FileName); var path = Path.Combine(Server.MapPath("~/Uploads/"), fileName); file[i].SaveAs(path); var photo = new DataLayer.Models.Photos(); photo.IdProduit = produits.IdProduit; photo.PhotoName = fileName.ToString(); db.Photos.Add(photo); db.SaveChanges(); } } return RedirectToAction("Index"); } ViewBag.CodePays = new SelectList(db.Pays, "CodeIso3", "Name", produits.CodePays); ViewBag.IdSousCategorie = new SelectList(db.SousCategories, "IdSousCategorie", "Nom", produits.IdSousCategorie); return View(produits); }
public ActionResult AddPicture([Bind(Include="IdProduit")] Produits produit, HttpPostedFileBase file) { bool pictureValide = true; if (file == null || file.ContentLength <= 0) { pictureValide = false; } var fileName = Path.GetFileName(file.FileName); if (fileName == null) { pictureValide = false; } if (pictureValide == true) { var path = Path.Combine(Server.MapPath("~/Uploads/"), fileName); file.SaveAs(path); var photo = new DataLayer.Models.Photos(); photo.IdProduit = produit.IdProduit; photo.PhotoName = file.FileName.ToString(); db.Photos.Add(photo); db.SaveChanges(); } return RedirectToAction("Edit", new { id = produit.IdProduit }); }
public ActionResult Create([Bind(Include = "IdProduit,IdVille,NbPlaces,Adresse,Description")] Produits produits, double Prix, HttpPostedFileBase postedFile, string e1) { if (e1 == null) { return RedirectToAction("Index"); } if (postedFile == null || postedFile.ContentLength <= 0) { return RedirectToAction("Index"); } var fileName = Path.GetFileName(postedFile.FileName); if(fileName == null) { return RedirectToAction("Index"); } var path = Path.Combine(Server.MapPath("~/Images/"),fileName); postedFile.SaveAs(path); string[] tab = e1.Trim().Split('-'); if (ModelState.IsValid) { db.Produits.Add(produits); db.SaveChanges(); var prix = new Prix() { IdProduit = produits.IdProduit, Montant = (int)Prix, DateDebut = Convert.ToDateTime(tab[0]), DateFin = Convert.ToDateTime(tab[1]) }; db.Prix.Add(prix); db.SaveChanges(); var photo = new Photos { IdProduit = produits.IdProduit, Path = path }; db.Photos.Add(photo); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.IdVille = new SelectList(db.Villes, "idVille", "name", produits.IdVille); return View(produits); }
public PartialViewResult PhotoProduit(string id) { int idProduit = int.Parse(id); List<DataLayer.Models.Photos> listPhoto = db.Photos.Where(c => c.IdProduit == idProduit).ToList(); if (listPhoto.Count()==0) { var photoNotFound = new DataLayer.Models.Photos(); photoNotFound.PhotoName = "pagenotfound_icon.png"; photoNotFound.IdProduit = idProduit; var listPhoto2 = new List<DataLayer.Models.Photos>(); listPhoto2.Add(photoNotFound); listPhoto = listPhoto2; } return PartialView("_PhotoProduit",listPhoto); }
/// <summary> /// Permet de modifier la photo d'un seul produit /// </summary> /// <param name="id">IdPhoto</param> /// <param name="id2">IdProduit</param> /// <returns></returns> public ActionResult ModifPhotos(string id, string id2) { int IdProduit = int.Parse(id2); int IdPhoto = int.Parse(id); var Photo = new DataLayer.Models.Photos(); if (IdPhoto == 0) { // Il n'y a pas de photo donc il faut creer la photo dans la base de donnée var photo = new DataLayer.Models.Photos(); photo.IdProduit = IdProduit; photo.PhotoName = "pagenotfound_icon.png"; db.Photos.Add(photo); db.SaveChanges(); IdPhoto = photo.IdPhoto; } Photo = db.Photos.Where(c => c.IdProduit == IdProduit && c.IdPhoto == IdPhoto) .First(); return View(Photo); }