/// <summary> /// Updates the game with adding/deleting/updating the related child entries in the DB /// </summary> /// <param name="newGame"></param> public static void UpdateGame(Game newGame) { Game oldGame = GetGameByTitle(newGame.Title); oldGame.Title = newGame.Title; oldGame.GamePlatforms.Clear(); oldGame.GameGenres.Clear(); for (int i = 0; i < newGame.GamePlatforms.Count; i++) { GamePlatform gp = new GamePlatform(); gp.Platform = newGame.GamePlatforms[i].Platform; gp.Price = newGame.GamePlatforms[i].Price; gp.InStock = newGame.GamePlatforms[i].InStock; oldGame.GamePlatforms.Add(gp); } for (int i = 0; i < newGame.GameGenres.Count; i++) { GameGenre gg = new GameGenre(); gg.Genre = newGame.GameGenres[i].Genre; oldGame.GameGenres.Add(gg); } _context.SaveChanges(); }
protected void BtnSubmit_Click(object sender, EventArgs e) { // validate the platform controls first Page.Validate("ValPlatform"); if (Page.IsValid) { Game game = new Game(); game.Title = TxtTitle.Text; game.Rating = DdlRating.SelectedValue; game.ReleaseYear = Convert.ToInt32(TxtYear.Text); for (int i = 0; i < CheckBoxList1.Items.Count; i++) { ListItem chk = CheckBoxList1.Items[i]; if (chk.Selected) { GameGenre gg = new GameGenre(); gg.Genre = chk.Text; game.GameGenres.Add(gg); } } // loop through the Panel1 control to find all platform related checkboxes for (int i = 0; i < Panel1.Controls.Count; i++) { if (Panel1.Controls[i] is CheckBox) { // loop through platform names to compare input field id's. for (int j = 0; j < platformFieldIds.Count; j++) { // generate the platform based id for comparing with checkbox id string chkId = "Chk" + platformFieldIds[j]; // get the proper controls from the html CheckBox chk = (CheckBox)Panel1.Controls[i]; // if checkbox is not null AND checkbox belongs to that specific platform AND checkbox is checked // then set the values of game object. if (chk != null && chk.ID.Equals(chkId) && chk.Checked) { string txtPriceId = "TxtPrice" + platformFieldIds[j]; string txtStockId = "TxtStock" + platformFieldIds[j]; TextBox txtPrice = (TextBox)FindControlRecursive(Panel1, txtPriceId); TextBox txtStock = (TextBox)FindControlRecursive(Panel1, txtStockId); GamePlatform gp = new GamePlatform(); gp.Platform = chk.Text; gp.Price = Convert.ToDouble(txtPrice.Text); gp.InStock = Convert.ToInt32(txtStock.Text); game.GamePlatforms.Add(gp); } } } } DataLayerAccess.AddGame(game); Response.Redirect("AddGame.aspx"); } }