// GET: Samourais/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Samourai samourai = db.Samourais.Include(s => s.ArtMartials).FirstOrDefault(s => s.Id == id); if (samourai == null) { return(HttpNotFound()); } List <SelectListItem> ArmesListItems = getArmesSelectListItems(samourai.Id); SamouraisViewModel vm = new SamouraisViewModel { Samourai = samourai, Armes = ArmesListItems, ArtMartials = db.ArtMartials.Select(a => new SelectListItem { Text = a.Nom, Value = a.Id.ToString() }).ToList() }; if (samourai.Arme != null) { vm.IdArme = samourai.Arme.Id; } if (samourai.ArtMartials != null && samourai.ArtMartials.Count() > 0) { vm.IdArtMartials = samourai.ArtMartials.Select(a => a.Id).ToList(); } return(View(vm)); }
// GET: Samourais/Create public ActionResult Create() { SamouraisViewModel vm = new SamouraisViewModel { Armes = db.Armes.Where(a => !db.Samourais.Select(s => s.Arme).Contains(a)).Select(a => new SelectListItem() { Text = a.Nom + " " + a.Degats, Value = a.Id.ToString() }).ToList(), ArtMartials = db.ArtMartials.Select(a => new SelectListItem { Text = a.Nom, Value = a.Id.ToString() }).ToList() }; return(View(vm)); }
public ActionResult Create(SamouraisViewModel vm) { if (ModelState.IsValid) { Samourai s = vm.Samourai; s.Arme = db.Armes.FirstOrDefault(a => a.Id == vm.IdArme); s.ArtMartials = db.ArtMartials.Where(a => vm.IdArtMartials.Contains(a.Id)).ToList(); db.Samourais.Add(s); db.SaveChanges(); return(RedirectToAction("Index")); } vm.Armes = getArmesSelectListItems(vm.Samourai.Id); vm.ArtMartials = db.ArtMartials.Select(a => new SelectListItem { Text = a.Nom, Value = a.Id.ToString() }).ToList(); return(View(vm)); }
public ActionResult Edit(SamouraisViewModel vm) { Samourai samourai = db.Samourais.Include(s => s.Arme).Include(s => s.ArtMartials).SingleOrDefault(s => s.Id == vm.Samourai.Id); if (ModelState.IsValid) { samourai.Force = vm.Samourai.Force; samourai.Nom = vm.Samourai.Nom; samourai.Arme = db.Armes.FirstOrDefault(a => a.Id == vm.IdArme); samourai.ArtMartials = db.ArtMartials.Where(a => vm.IdArtMartials.Contains(a.Id)).ToList(); db.Entry(samourai).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } vm.Armes = getArmesSelectListItems(samourai.Id); vm.ArtMartials = db.ArtMartials.Select(a => new SelectListItem { Text = a.Nom, Value = a.Id.ToString() }).ToList(); return(View(vm)); }