public async Task <IActionResult> Create([Bind("strength", "agility", "intellect", "will")] IFormCollection collection) { //get current user ApplicationUser user = await GetCurrentUserAsync(); //get last character for current user var characters = _context.Characters .Where(character => character.ApplicationUserId == user.Id) .Last(); //Assign form values to variables var strenghtValue = collection.ElementAt(0).Value; var agilityValue = collection.ElementAt(1).Value; var intellectValue = collection.ElementAt(2).Value; var WillValue = collection.ElementAt(3).Value; //Method for validation using form variables int TotalValue() { //Parse from string to int int numVal = Int32.Parse(strenghtValue) + Int32.Parse(agilityValue) + Int32.Parse(intellectValue) + Int32.Parse(WillValue); return(numVal); } if (ModelState.IsValid && TotalValue() == 42) { //build new row in CharTrait table CharTrait humanTraitStrength = new CharTrait { CharacterId = characters.CharacterId, TraitId = 1, CharTraitValue = collection.ElementAt(0).Value }; //add to hold in db context _context.Add(humanTraitStrength); CharTrait humanTraitAgility = new CharTrait { CharacterId = characters.CharacterId, TraitId = 2, CharTraitValue = collection.ElementAt(1).Value }; //add to hold in db context _context.Add(humanTraitAgility); CharTrait humanTraitIntellect = new CharTrait { CharacterId = characters.CharacterId, TraitId = 3, CharTraitValue = collection.ElementAt(2).Value }; //add to hold in db context _context.Add(humanTraitIntellect); CharTrait humanTraitWill = new CharTrait { CharacterId = characters.CharacterId, TraitId = 4, CharTraitValue = collection.ElementAt(3).Value }; //add to hold in db context and save context to db _context.Add(humanTraitWill); await _context.SaveChangesAsync(); return(RedirectToAction("UserHome", "ApplicationUser")); } else { //if failed vaildation, return to form return(View("HumanAbilitiesForm")); } }
public async Task <IActionResult> Create([Bind("CharacterId,CharacterName,Gender,Level,ApplicationUserId,AncestryId")] Character character) { if (ModelState.IsValid) { //get current user and set user property on character to user ApplicationUser user = await GetCurrentUserAsync(); character.ApplicationUserId = user.Id; //connection to veiw model CharacterCreateViewModel model = new CharacterCreateViewModel(_context); //make dropdown and assign to property on viewmodel model.AncestriesList = new SelectList(_context.Ancestry, "AncestryId", "AncestryId", character.AncestryId); //assign to property character on model model.Character = character; //if user selects non-human _context.Add(character); await _context.SaveChangesAsync(); //if User select Human if (model.Character.AncestryId == 1) { return(RedirectToAction("HumanAbilitiesForm", "HumanAbilities")); } else if (model.Character.AncestryId == 2) { var abt = (from ab in _context.AncestryBaseTraits join a in _context.Ancestry on ab.AncestryId equals a.AncestryId where ab.AncestryId == 2 select ab.BaseValue) .ToList(); CharTrait changelingTraitStrength = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 1, CharTraitValue = abt.ElementAt(0).ToString() }; //add to hold in db context _context.Add(changelingTraitStrength); CharTrait changelingTraitAgility = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 2, CharTraitValue = abt.ElementAt(1).ToString() }; //add to hold in db context _context.Add(changelingTraitAgility); CharTrait changelingTraitIntellect = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 3, CharTraitValue = abt.ElementAt(2).ToString() }; //add to hold in db context _context.Add(changelingTraitIntellect); CharTrait changelingTraitWill = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 4, CharTraitValue = abt.ElementAt(3).ToString() }; //add to hold in db context and save context to db _context.Add(changelingTraitWill); await _context.SaveChangesAsync(); return(RedirectToAction("UserHome", "ApplicationUser")); } //begin clockwork if (model.Character.AncestryId == 3) { var abt = (from ab in _context.AncestryBaseTraits join a in _context.Ancestry on ab.AncestryId equals a.AncestryId where ab.AncestryId == 3 select ab.BaseValue) .ToList(); CharTrait clockworkTraitStrength = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 1, CharTraitValue = abt.ElementAt(0).ToString() }; //add to hold in db context _context.Add(clockworkTraitStrength); CharTrait clockworkTraitAgility = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 2, CharTraitValue = abt.ElementAt(1).ToString() }; //add to hold in db context _context.Add(clockworkTraitAgility); CharTrait clockworkTraitIntellect = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 3, CharTraitValue = abt.ElementAt(2).ToString() }; //add to hold in db context _context.Add(clockworkTraitIntellect); CharTrait clockworkTraitWill = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 4, CharTraitValue = abt.ElementAt(3).ToString() }; //add to hold in db context and save context to db _context.Add(clockworkTraitWill); await _context.SaveChangesAsync(); return(RedirectToAction("Create", "ClockworkPurposes")); } //end clockwork //begin dwarf if (model.Character.AncestryId == 4) { var abt = (from ab in _context.AncestryBaseTraits join a in _context.Ancestry on ab.AncestryId equals a.AncestryId where ab.AncestryId == 4 select ab.BaseValue) .ToList(); CharTrait dwarfTraitStrength = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 1, CharTraitValue = abt.ElementAt(0).ToString() }; //add to hold in db context _context.Add(dwarfTraitStrength); CharTrait dwarfTraitAgility = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 2, CharTraitValue = abt.ElementAt(1).ToString() }; //add to hold in db context _context.Add(dwarfTraitAgility); CharTrait dwarfTraitIntellect = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 3, CharTraitValue = abt.ElementAt(2).ToString() }; //add to hold in db context _context.Add(dwarfTraitIntellect); CharTrait dwarfTraitWill = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 4, CharTraitValue = abt.ElementAt(3).ToString() }; //add to hold in db context and save context to db _context.Add(dwarfTraitWill); await _context.SaveChangesAsync(); return(RedirectToAction("UserHome", "ApplicationUser")); } //end dwarf //begin goblin if (model.Character.AncestryId == 5) { var abt = (from ab in _context.AncestryBaseTraits join a in _context.Ancestry on ab.AncestryId equals a.AncestryId where ab.AncestryId == 5 select ab.BaseValue) .ToList(); CharTrait goblinTraitStrength = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 1, CharTraitValue = abt.ElementAt(0).ToString() }; //add to hold in db context _context.Add(goblinTraitStrength); CharTrait goblinTraitAgility = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 2, CharTraitValue = abt.ElementAt(1).ToString() }; //add to hold in db context _context.Add(goblinTraitAgility); CharTrait goblinTraitIntellect = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 3, CharTraitValue = abt.ElementAt(2).ToString() }; //add to hold in db context _context.Add(goblinTraitIntellect); CharTrait goblinTraitWill = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 4, CharTraitValue = abt.ElementAt(3).ToString() }; //add to hold in db context and save context to db _context.Add(goblinTraitWill); await _context.SaveChangesAsync(); return(RedirectToAction("UserHome", "ApplicationUser")); } //end goblin //begin orc if (model.Character.AncestryId == 6) { var abt = (from ab in _context.AncestryBaseTraits join a in _context.Ancestry on ab.AncestryId equals a.AncestryId where ab.AncestryId == 6 select ab.BaseValue) .ToList(); CharTrait orcTraitStrength = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 1, CharTraitValue = abt.ElementAt(0).ToString() }; //add to hold in db context _context.Add(orcTraitStrength); CharTrait orcTraitAgility = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 2, CharTraitValue = abt.ElementAt(1).ToString() }; //add to hold in db context _context.Add(orcTraitAgility); CharTrait orcTraitIntellect = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 3, CharTraitValue = abt.ElementAt(2).ToString() }; //add to hold in db context _context.Add(orcTraitIntellect); CharTrait orcTraitWill = new CharTrait { CharacterId = model.Character.CharacterId, TraitId = 4, CharTraitValue = abt.ElementAt(3).ToString() }; //add to hold in db context and save context to db _context.Add(orcTraitWill); await _context.SaveChangesAsync(); return(RedirectToAction("UserHome", "ApplicationUser")); } //end orc return(View(character)); } return(View()); }