public ActionResult Edit(SamouraiVM vm) { if (ModelState.IsValid) { var samouraiDb = db.Samourais.Find(vm.Samourai.Id); samouraiDb.Force = vm.Samourai.Force; samouraiDb.Nom = vm.Samourai.Nom; if (vm.IdArme != null) { var ArmeProprio = db.Samourais.Where(s => s.Arme.Id == vm.IdArme).ToList(); Arme arme = null; foreach (var item in ArmeProprio) { arme = item.Arme; item.Arme = null; db.Entry(item).State = EntityState.Modified; } if (arme == null) { samouraiDb.Arme = db.Armes.FirstOrDefault(a => a.Id == vm.IdArme); } else { samouraiDb.Arme = arme; } } else { samouraiDb.Arme = null; } foreach (var item in samouraiDb.ArtMartiaux) { db.Entry(item).State = EntityState.Modified; } samouraiDb.ArtMartiaux = db.ArtMartials.Where(am => vm.IdArtMartiaux.Contains(am.Id)).ToList(); db.Entry(samouraiDb).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } List <int> armesIds = db.Samourais.Where(s => s.Arme != null && s.Id != vm.Samourai.Id).Select(s => s.Arme.Id).ToList(); vm.Armes = db.Armes.Where(s => !armesIds.Contains(s.Id)).ToList(); return(View(vm)); }
public ActionResult Edit(SamouraiVM vm) { if (ModelState.IsValid) { //var sam = db.Samourais.Include(x => x.Arme).FirstOrDefault(x => x.Id == vm.Samourai.Id); Samourai sam = db.Samourais.Include(s => s.Arme).FirstOrDefault(s => s.Id == vm.Samourai.Id); sam.Artmartials.Clear(); sam.Force = vm.Samourai.Force; sam.Nom = vm.Samourai.Nom; if (vm.ArmesId != null) { sam.Arme = db.Armes.Find(vm.ArmesId); } else { sam.Arme = null; } if (vm.ArtMartialsId != null) { foreach (var item in vm.ArtMartialsId) { sam.Artmartials = db.ArtMartials.Where(x => vm.ArtMartialsId.Contains(x.Id)).ToList(); } } db.Entry(sam).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(vm)); }
public ActionResult Edit([Bind(Include = "Id,Nom,Degats")] Arme arme) { if (ModelState.IsValid) { db.Entry(arme).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(arme)); }
public ActionResult Edit([Bind(Include = "Id,Force,Nom")] Samourai samourai) { if (ModelState.IsValid) { db.Entry(samourai).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(samourai)); }
public ActionResult Edit(VModelSamourai vm) { if (ModelState.IsValid) { vm.Samourai.Arme = db.Armes.Find(vm.ArmeId); db.Entry(vm.Samourai).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(vm)); }
public ActionResult Edit(SamuraiViewModel svm) { var armesUsedIds = db.Samurais.Where(x => x.Arme != null && x.Id != svm.Samurai.Id).Select(x => x.Arme.Id).ToList(); svm.armes = db.Armes.Where(a => !armesUsedIds.Contains(a.Id)).Select(a => a).ToList(); svm.ArtMartials = db.ArtMartials.ToList(); if (ModelState.IsValid && SamuraiValidator.Validate(svm, ModelState, db)) { db.Entry(svm.Samurai).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(svm)); }
public ActionResult Edit(SamouraiViewModel vm) { if (ModelState.IsValid) { var samourai = db.Samourais.Include(s => s.Arme).SingleOrDefault(s => s.Id == vm.Samourai.Id); samourai.Arme = db.Armes.Find(vm.ArmeIdSelected); samourai.Nom = vm.Samourai.Nom; samourai.Force = vm.Samourai.Force; db.Entry(samourai).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(GetSamouraiViewModel(vm.Samourai))); }
public ActionResult Edit(SamouraiVM samouraiVM) { if (ModelState.IsValid) { Samourai samouraiDB = db.Samourais.Find(samouraiVM.Samourai.Id); samouraiDB.Arme = null; if (samouraiVM.IdSelected.HasValue) { samouraiDB.Arme = db.Armes.FirstOrDefault(a => a.Id == samouraiVM.IdSelected); } samouraiDB.Nom = samouraiVM.Samourai.Nom; samouraiDB.Force = samouraiVM.Samourai.Force; db.Entry(samouraiDB).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } samouraiVM.Armes = db.Armes.ToList(); return(View(samouraiVM)); }
public ActionResult Edit(SamouraiViewModel vm) { if (ModelState.IsValid) { var samourai = db.Samourais.Include(s => s.Arme).SingleOrDefault(s => s.Id == vm.Samourai.Id); samourai.Arme = db.Armes.Find(vm.ArmeIdSelected); samourai.Nom = vm.Samourai.Nom; samourai.Force = vm.Samourai.Force; if (vm.ListeIdArtsMartiauxSelected != null) { samourai.ArtsMartiaux = new List <ArtMartial>(); samourai.ArtsMartiaux = db.ArtsMartiaux.Where(x => vm.ListeIdArtsMartiauxSelected.Contains(x.Id)).ToList(); } db.Entry(samourai).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(GetSamouraiViewModel(vm.Samourai))); }