public IActionResult Update(int id) { DbSet <MugOrder> dbs = _dbContext.MugOrder; MugOrder tOrder = dbs.Where(mo => mo.Id == id).FirstOrDefault(); if (tOrder != null) { // other code... DbSet <Pokedex> dbsPoke = _dbContext.Pokedex; var lstPokes = dbsPoke.ToList <Pokedex>() .OrderBy(p => p.Name) .Select( p => { dynamic d = new ExpandoObject(); d.value = p.Id; d.text = p.Name; return(d); } ) .ToList <dynamic>(); ViewData["pokes"] = lstPokes; return(View(tOrder)); } else { TempData["Msg"] = "Mug order not found!"; return(RedirectToAction("Index")); } }
public IActionResult Update(int id) { // TODO P09 Task 3-3: Assign dbs to _dbContext.MugOrder DbSet <MugOrder> dbs = _dbContext.MugOrder; // TODO P09 Task 3-4: User dbs.Where and FirstOrDefault methods to retrieve the targetted MugOrder MugOrder tOrder = dbs.Where(mo => mo.Id == id).FirstOrDefault(); if (tOrder != null) { DbSet <Pokedex> dbsPoke = _dbContext.Pokedex; var lstPokes = dbsPoke.ToList <Pokedex>() .OrderBy(p => p.Name) .Select( p => { dynamic d = new ExpandoObject(); d.value = p.Id; d.text = p.Name; return(d); } ) .ToList <dynamic>(); ViewData["pokes"] = lstPokes; return(View(tOrder)); } else { TempData["Msg"] = "Mug order not found!"; return(RedirectToAction("Index")); } }
public IActionResult Delete(int id) { DbSet <MugOrder> dbs = _dbContext.MugOrder; // TODO P10 Task 2-2 Use Where and FirstOrDefault LINQ-to-Entities method to retrieve the target MugOrder object //MugOrder tOrder = null; // this line needs to be modified to achieve Task 2-2 MugOrder tOrder = dbs.Where(mo => mo.Id == id) .FirstOrDefault(); if (tOrder != null) { // TODO P10 Task 2-3 Use Remove method to remove the found MugOrder object from DbSet dbs.Remove(tOrder); if (_dbContext.SaveChanges() == 1) { TempData["Msg"] = "Mug order deleted!"; } else { TempData["Msg"] = "Failed to update database!"; } } else { TempData["Msg"] = "Mug order not found!"; } return(RedirectToAction("Index")); }
public IActionResult Update(MugOrder mugOrder) { if (ModelState.IsValid) { DbSet <MugOrder> dbs = _dbContext.MugOrder; MugOrder tOrder = dbs.Where(mo => mo.Id == mugOrder.Id) .FirstOrDefault(); if (tOrder != null) { tOrder.Color = mugOrder.Color; tOrder.PokedexId = mugOrder.PokedexId; tOrder.Qty = mugOrder.Qty; if (_dbContext.SaveChanges() == 1) { TempData["Msg"] = "Mug order updated!"; } else { TempData["Msg"] = "Failed to update database!"; } } else { TempData["Msg"] = "Mug order not found!"; return(RedirectToAction("Index")); } } else { TempData["Msg"] = "Invalid information entered"; } return(RedirectToAction("Index")); }
public IActionResult LoadMug(string color, int pokedexid) { // TODO P10 Task 3-1 Complete the following code to create a new MugOrder and assign the pokedexid and color to the model properties //MugOrder model = null; // this line needs to be modified to achieve Task 3-1 MugOrder model = new MugOrder(); model.Color = color; model.PokedexId = pokedexid; // TODO P10 Task 3-2 Use PartialView method to return _ShowMug partial view and pass model object as the model //return null; // this line needs to be modified to achieve Task 3-2 return(PartialView("_ShowMug", model)); }
public IActionResult Create(MugOrder mugOrder) { if (ModelState.IsValid) { DbSet <MugOrder> dbs = _dbContext.MugOrder; mugOrder.CreatedBy = User.FindFirst(ClaimTypes.NameIdentifier).Value; dbs.Add(mugOrder); if (_dbContext.SaveChanges() == 1) { TempData["Msg"] = "New order added!"; } else { TempData["Msg"] = "Failed to update database!"; } } else { TempData["Msg"] = "Invalid information entered"; } return(RedirectToAction("Index")); }
public IActionResult Update(MugOrder mugOrder) { if (ModelState.IsValid) { // TODO P09 Task 3-5: Repeat task 3-3 and 3-4 DbSet <MugOrder> dbs = _dbContext.MugOrder; MugOrder tOrder = dbs.Where(mo => mo.Id == mugOrder.Id).FirstOrDefault(); if (tOrder != null) { // TODO P09 Task 3-6: Assign mugOrder's property values to tOrder's properties // Note: Assign current login user's Id to CreatedBy column tOrder.Color = mugOrder.Color; // this is done for you tOrder.PokedexId = mugOrder.PokedexId; tOrder.Qty = mugOrder.Qty; tOrder.CreatedBy = User.FindFirst(ClaimTypes.NameIdentifier).Value; // TODO P09 Task 3-7: Update database using _dbContext and display appropriate message if (_dbContext.SaveChanges() == 1) { TempData["Msg"] = "Mug order updated!"; } else { TempData["Msg"] = "Failed to update database!"; } } else { TempData["Msg"] = "Mug order not found!"; return(RedirectToAction("Index")); } } else { TempData["Msg"] = "Invalid information entered"; } return(RedirectToAction("Index")); }