public ActionResult Edit(HeroViewModel vmodel) { if (ModelState.IsValid) { bool isUniversal = TranslationBL.CheckIfUniversal(vmodel.HeroesTrans); db.Entry(vmodel.Hero).State = EntityState.Modified; foreach (var item in vmodel.HeroesTrans) { if (item.nameHero != null) { if (isUniversal) { item.languageId = LanguageBL.FindIdLanguageByShortForm("all"); db.Entry(item).State = EntityState.Modified; } else { db.Entry(item).State = EntityState.Modified; } } } db.SaveChanges(); return(RedirectToAction("Index")); } return(View(vmodel)); }
public ActionResult Create(HeroViewModel vmodel) { if (ModelState.IsValid) { //Check if nameHero have min 1 value IList <HERO_TRANSLATION> heroesT = vmodel.HeroesTrans; bool nameHeroIsValid = TranslationBL.CheckIfMinOneValued(heroesT); if (nameHeroIsValid) { //Check if Hero exist by nameHero before create bool nameExist = TranslationBL.CheckIfNameExist(heroesT); if (!nameExist) { HERO hero = new HERO { imageHero = vmodel.ImageHero }; db.HEROs.Add(hero); db.SaveChanges(); int id = hero.idHero; int count = heroesT.Count(); //Check if nameHero isUniversal bool isUniversal = TranslationBL.CheckIfUniversal(heroesT); if (isUniversal) { for (int i = 0; i < heroesT.Count(); i++) { if (heroesT[i].nameHero != null) { heroesT[i].heroId = id; //change language with universal heroesT[i].languageId = LanguageBL.FindIdLanguageByShortForm("all"); } else { heroesT.Remove(heroesT[i]); i--; } } } else { foreach (var item in heroesT) { item.heroId = id; } } db.HERO_TRANSLATIONs.AddRange(heroesT); db.SaveChanges(); return(RedirectToAction("Index")); } else { //to do --> match the error with the name that causes the error ViewBag.nameIsNotValid = "Le nom existe déjà, veuillez saisir un autre nom!"; } } else { ViewBag.nameIsNotValid = "Veuillez saisir un nom!"; } } vmodel.ListLang = LanguageBL.FindLanguageListWithoutUniversal(); return(View(vmodel)); }