public ActionResult Edit(int id) { var service = CreateMonstersService(); var detail = service.GetMonsterById(id); var model = new MonstersEdit { MonsterId = detail.MonsterId, MonsterName = detail.MonsterName, MonsterDesc = detail.MonsterDesc, MonsterLevel = detail.MonsterLevel }; return(View(model)); }
public bool UpdateMonster(MonstersEdit model) { using (var ctx = new ApplicationDbContext()) { var entity = ctx .Monsters .Single(e => e.MonsterId == model.MonsterId && e.OwnerId == _userId); entity.MonsterId = model.MonsterId; entity.MonsterName = model.MonsterName; entity.MonsterDesc = model.MonsterDesc; entity.MonsterLevel = model.MonsterLevel; return(ctx.SaveChanges() == 1); } }
public ActionResult Edit(int id, MonstersEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.MonsterId != id) { ModelState.AddModelError("", "Id Mismatch"); return(View(model)); } var service = CreateMonstersService(); if (service.UpdateMonster(model)) { TempData["SaveResult"] = $"{model.MonsterName} has been successfully updated"; return(RedirectToAction("Index")); } ModelState.AddModelError("", $"{model.MonsterName} could not be updated"); return(View(model)); }