using System.Net; using System.Web.Mvc; using CD67.FicheCollege.Entity; using CD67.FicheCollege.Factory; using CD67.FicheCollege.MVC.Models; using System.Linq; using System.Collections.Generic; using System; namespace CD67.FicheCollege.MVC.Controllers { public class RestaurationParametresController : Controller { private Entities db = new Entities(); // GET: RestaurationParametres/Edit/5 public ActionResult Edit(int? annee_id) { if (annee_id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } AnneeFactory fact = new AnneeFactory(db); Annee annee = fact.getById(annee_id); RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db); RestaurationParametre param = paramFactory.getBy(x => x.Annee_Id == annee_id.Value); RestaurationParametreViewModel model; if (param == null) { param = new RestaurationParametre(); param.Annee_Id = annee_id.Value; } RestaurationTypeRepaFactory typeRepasFactory = new RestaurationTypeRepaFactory(db); List repasCBList = new List(); foreach (var item in typeRepasFactory.getAllValid().ToList()) { repasCBList.Add( new CheckBoxListItem() { ID = item.Id.ToString(), Display = item.Libelle, IsChecked = param.RestaurationTypeRepas.Any(i => i.Id == item.Id) ? true : false } ); } ViewBag.repasCBList = repasCBList; param.Annee = annee; if (param.Id == 0) model = new RestaurationParametreViewModel(param, ModeAcces.Creation); else model = new RestaurationParametreViewModel(param, ModeAcces.Modification); return View(model); } // POST: RestaurationParametres/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.RestaurationParametre param, List RepasCBList) { ModeAcces mode = new ModeAcces(); RestaurationTypeRepaFactory typeRepasFactory = new RestaurationTypeRepaFactory(db); if (ModelState.IsValid) { db.RestaurationParametres.Attach(param); //Récupération des types de repas cochés db.Entry(param).Collection(i => i.RestaurationTypeRepas).Load(); for (int i = 0; i < RepasCBList.Count; i++) { Models.CheckBoxListItem item = RepasCBList[i]; if (item.IsChecked) { if (!param.RestaurationTypeRepas.Any(repas => repas.Id == int.Parse(item.ID))) { param.RestaurationTypeRepas.Add(typeRepasFactory.getById(int.Parse(item.ID))); } } else { if (param.RestaurationTypeRepas.Any(repas => repas.Id == int.Parse(item.ID))) { param.RestaurationTypeRepas.Remove(typeRepasFactory.getById(int.Parse(item.ID))); } } } RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db); if (param.Id == 0) { paramFactory.add(ref param); mode = ModeAcces.Creation; } else { paramFactory.update(ref param); mode = ModeAcces.Modification; } return RedirectToAction("Index", "Restauration", new { annee_id = param.Annee_Id }); } List repasCBList = new List(); foreach (var item in typeRepasFactory.getAllValid().ToList()) { repasCBList.Add( new CheckBoxListItem() { ID = item.Id.ToString(), Display = item.Libelle, IsChecked = param.RestaurationTypeRepas.Any(i => i.Id == item.Id) ? true : false } ); } ViewBag.repasCBList = repasCBList; RestaurationParametreViewModel model = new RestaurationParametreViewModel(param, mode); return View(model); } // GET: RestaurationParametres/EditCampagne/5 public ActionResult EditCampagne(int? annee_id, bool edit) { if (annee_id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } RestaurationParametreFactory paramFactory = new RestaurationParametreFactory(db); RestaurationParametre param = paramFactory.getBy(x => x.Annee_Id == annee_id.Value); if (param == null) { param = new RestaurationParametre(); param.Annee_Id = annee_id.Value; param.Campagne = edit; paramFactory.add(ref param); } else { param.Campagne = edit; paramFactory.update(ref param); } return RedirectToAction("Index", "Restauration", new { annee_id = param.Annee_Id }); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }