public IActionResult Warcaster(EditWarcasterViewModel editModel) { Warcaster editWarcaster = context.Warcasters.Single(c => c.ID == editModel.soloID); editWarcaster.ARM = editModel.ARM; editWarcaster.CMD = editModel.CMD; editWarcaster.DEF = editModel.DEF; editWarcaster.FA = editModel.FA; editWarcaster.MAT = editModel.MAT; editWarcaster.RAT = editModel.RAT; editWarcaster.Name = editModel.Name; editWarcaster.SPD = editModel.SPD; editWarcaster.PointCost = editModel.PointCost; editWarcaster.STR = editModel.STR; editWarcaster.factionName = editModel.Faction; editWarcaster.Feat = editModel.Feat; context.SaveChanges(); editModel.currenntAbilIDs = context.WarcasterAbilities.Where(c => c.WarcasterId == editWarcaster.ID).Select(x => x.AbilityId).ToList(); editModel.currenntSpellIDs = context.WarcasterSpells.Where(c => c.WarcasterId == editWarcaster.ID).Select(x => x.SpellId).ToList(); editModel.currenntWeaponIDs = context.WarcasterWeapons.Where(c => c.WarcsaterId == editWarcaster.ID).Select(x => x.WeaponId).ToList(); if (editModel.abilIDS != null) { foreach (var abil in editModel.abilIDS) { if (!editModel.currenntAbilIDs.Contains(abil)) { WarcasterAbility NewSoloAbility = new WarcasterAbility(); NewSoloAbility.AbilityId = abil; NewSoloAbility.WarcasterId = editModel.soloID; context.WarcasterAbilities.Add(NewSoloAbility); context.SaveChanges(); } foreach (var currentAbil in editModel.currenntAbilIDs) { if (!editModel.abilIDS.Contains(currentAbil)) { WarcasterAbility soloabil = (from s in context.WarcasterAbilities where s.AbilityId == currentAbil where s.WarcasterId == editModel.soloID select s).FirstOrDefault <WarcasterAbility>(); context.WarcasterAbilities.Remove(soloabil); context.SaveChanges(); } } } } else { // delete all solo abils var soloAbils = context.WarcasterAbilities.Where(c => c.WarcasterId == editModel.soloID).ToList(); foreach (var Abil in soloAbils) { context.WarcasterAbilities.Remove(Abil); context.SaveChanges(); } } if (editModel.weapIDS != null) { foreach (var weap in editModel.weapIDS) { if (!editModel.currenntWeaponIDs.Contains(weap)) { WarcasterWeapon NewSoloWeapon = new WarcasterWeapon(); NewSoloWeapon.WeaponId = weap; NewSoloWeapon.WarcsaterId = editModel.soloID; context.WarcasterWeapons.Add(NewSoloWeapon); context.SaveChanges(); } foreach (var weaps in editModel.currenntWeaponIDs) { if (!editModel.weapIDS.Contains(weaps)) { WarcasterWeapon soloWeap = (from s in context.WarcasterWeapons where s.WeaponId == weap where s.WarcsaterId == editModel.soloID select s).FirstOrDefault <WarcasterWeapon>(); context.WarcasterWeapons.Remove(soloWeap); context.SaveChanges(); } } } } else { // delete all solo abils var soloWeaps = context.WarcasterWeapons.Where(c => c.WarcsaterId == editModel.soloID).ToList(); foreach (var Weap in soloWeaps) { context.WarcasterWeapons.Remove(Weap); context.SaveChanges(); } } if (editModel.spellIDS != null) { foreach (var spell in editModel.spellIDS) { if (!editModel.currenntSpellIDs.Contains(spell)) { WarcasterSpell NewSoloSpell = new WarcasterSpell(); NewSoloSpell.SpellId = spell; NewSoloSpell.WarcasterId = editModel.soloID; context.WarcasterSpells.Add(NewSoloSpell); context.SaveChanges(); } foreach (var spells in editModel.currenntSpellIDs) { if (!editModel.spellIDS.Contains(spells)) { WarcasterSpell soloSpell = (from s in context.WarcasterSpells where s.SpellId == spell where s.WarcasterId == editModel.soloID select s).FirstOrDefault <WarcasterSpell>(); context.WarcasterSpells.Remove(soloSpell); context.SaveChanges(); } } } } else { // delete all solo abils var soloSpells = context.WarcasterSpells.Where(c => c.WarcasterId == editModel.soloID).ToList(); foreach (var Spell in soloSpells) { context.WarcasterSpells.Remove(Spell); context.SaveChanges(); } } return(Redirect("/View/Warcaster/" + editModel.soloID)); }
public IActionResult Warcaster(AddWarcasterViewModel model) { if (ModelState.IsValid) { Warcaster newSolo = new Warcaster(); newSolo.Name = model.Name; newSolo.ARM = model.ARM; newSolo.CMD = model.CMD; newSolo.DEF = model.DEF; newSolo.MAT = model.MAT; newSolo.PointCost = model.PointCost; newSolo.RAT = model.RAT; newSolo.SPD = model.SPD; newSolo.STR = model.STR; newSolo.WarjackPoints = model.WarjackPoints; newSolo.factionName = model.Faction; context.Warcasters.Add(newSolo); context.SaveChanges(); if (model.abilIDS != null) { foreach (var abil in model.abilIDS) { WarcasterAbility NewSoloAbility = new WarcasterAbility(); NewSoloAbility.AbilityId = abil; NewSoloAbility.WarcasterId = newSolo.ID; context.WarcasterAbilities.Add(NewSoloAbility); context.SaveChanges(); } } if (model.weapIDS != null) { foreach (var weap in model.weapIDS) { WarcasterWeapon NewSoloWeapon = new WarcasterWeapon(); NewSoloWeapon.WeaponId = weap; NewSoloWeapon.WarcsaterId = newSolo.ID; context.WarcasterWeapons.Add(NewSoloWeapon); context.SaveChanges(); } } if (model.spellIDS != null) { foreach (var spell in model.spellIDS) { WarcasterSpell NewSoloSpell = new WarcasterSpell(); NewSoloSpell.SpellId = spell; NewSoloSpell.WarcasterId = newSolo.ID; context.WarcasterSpells.Add(NewSoloSpell); context.SaveChanges(); } } return(Redirect("/")); } return(View("Warcaster", model)); }