public ActionResult Create(CategoryViewModel vmodel) { if (ModelState.IsValid) { //Check if nameHero have min 1 value IList <CATEGORY_TRANSLATION> catsT = vmodel.CategoriesTrans; bool nameCatIsValid = TranslationBL.CheckIfMinOneValued(catsT); if (nameCatIsValid) { //Check if Cat exist by nameCategory before create bool nameExist = TranslationBL.CheckIfNameExist(catsT); if (!nameExist) { CATEGORY cat = new CATEGORY { imageCat = vmodel.Image }; db.CATEGORYs.Add(cat); db.SaveChanges(); //Add Translation int id = cat.idCategory; IList <CATEGORY_TRANSLATION> newcatsT = TranslationBL.VerifyIsUniversal(catsT, id); db.CATEGORY_TRANSLATIONs.AddRange(newcatsT); db.SaveChanges(); if (vmodel.Parent != null) { SUBCATEGORY subCat = new SUBCATEGORY { parentCategory = vmodel.Parent.Value, childCategory = cat.idCategory }; db.SUBCATEGORYs.Add(subCat); 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!"; } } int lang = LanguageBL.CheckLanguageSession(); vmodel.ListLang = LanguageBL.FindLanguageListWithoutUniversal(); vmodel.Categories = db.SPP_CategoryTransDistinct(lang).ToList(); 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)); }