using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using CD67.FicheCollege.Entity; using CD67.FicheCollege.Factory; using CD67.FicheCollege.MVC.Models; using CD67.FicheCollege.MVC.Internal; namespace CD67.FicheCollege.MVC.Controllers { public class CollegesController : Controller { private Entities db = new Entities(); // GET: College public ActionResult Index(int? annee_id) { if (annee_id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } TopModel model = new TopModel(db.Colleges.Where(c=>c.Annee_Id == annee_id), ModeAcces.Lecture); return View(model); } // GET: College/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } CollegeFactory collegeFactory = new CollegeFactory(db); Entity.College college = collegeFactory.getById(id); if (college == null) { return HttpNotFound(); } TopModel model = new TopModel(college, ModeAcces.Lecture); return View(model); } // GET: College/Create public ActionResult Create() { Entity.College college = new Entity.College(); TopModel model = new TopModel(college, ModeAcces.Creation); model.Bag["Select_TypeCollege"] = new SelectList(db.TypesCollege.OrderBy(t=>t.Ordre).ToList()); model.Bag["Select_CodePostaux"] = new SelectList(Referentiel.GetCodesPostaux(college.Commune_Insee)); return View("Edit", model); } // POST: College/Create // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Entity.College college, string commentaire) { if (ModelState.IsValid) { CollegeFactory collegeFactory = new CollegeFactory(db); collegeFactory.add(ref college); return RedirectToAction("Index"); } TopModel model = new TopModel(college, ModeAcces.Creation); model.Bag["Select_TypeCollege"] = new SelectList(db.TypesCollege.ToList(), college.TypeCollege_Id); model.Bag["Select_CodePostaux"] = new SelectList(Referentiel.GetCodesPostaux(college.Commune_Insee), college.Commune_Insee); return View("Edit", model); } // GET: College/Edit/5 public ActionResult Edit(int? id) { CollegeFactory collegeFactory = new CollegeFactory(db); Entity.College college = collegeFactory.getById(id); if (college == null) { return HttpNotFound(); } TopModel model = new TopModel(college, ModeAcces.Modification); model.Bag["Select_TypeCollege"] = new SelectList(db.TypesCollege.ToList(), college.TypeCollege_Id); model.Bag["Select_CodePostaux"] = new SelectList(Referentiel.GetCodesPostaux(college.Commune_Insee), college.Commune_Insee); return View(model); } // POST: College/Edit/5 // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour // plus de détails, voir http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Entity.College college) { if (ModelState.IsValid) { CollegeFactory collegeFactory = new CollegeFactory(db); collegeFactory.update(ref college); return RedirectToAction("Details", new { Id = college.Id }); } TopModel model = new TopModel(college, ModeAcces.Modification); model.Bag["Select_TypeCollege"] = new SelectList(db.TypesCollege.ToList(), college.TypeCollege_Id); model.Bag["Select_CodePostaux"] = new SelectList(Referentiel.GetCodesPostaux(college.Commune_Insee), college.Commune_Insee); return View(model); } // GET: College/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } CollegeFactory collegeFactory = new CollegeFactory(db); Entity.College college = collegeFactory.getById(id); if (college == null) { return HttpNotFound(); } TopModel model = new TopModel(college, ModeAcces.Suppression); model.Bag["Select_TypeCollege"] = new SelectList(db.TypesCollege.ToList(), college.TypeCollege_Id); model.Bag["Select_CodePostaux"] = new SelectList(Referentiel.GetCodesPostaux(college.Commune_Insee), college.Commune_Insee); return View("Details", model); } // POST: College/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(string id) { CollegeFactory collegeFactory = new CollegeFactory(db); Entity.College college = collegeFactory.getById(id); collegeFactory.delete(ref college); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }