public virtual ActionResult Create(ItemCreateStartVM vm) { try { Item item = Mapper.Map<ItemCreateVM, Item>(vm.Item); using (var ctx = new Desert.DAL.Context()) { ItemType type = ctx.ItemTypes.Single(x => x.Id == vm.Item.TypeId); item.Type = type; ctx.Items.Add(item); ctx.SaveChanges(); } return RedirectToAction(ActionNames.Index); } catch { this.ModelState.AddModelError("", "error"); vm.Types = GetItemTypesSelectList(vm.Item.TypeId); return View(vm); } }
public virtual ActionResult Delete(Guid id, FormCollection form) { try { using (var ctx = new Desert.DAL.Context()) { var item = ctx.Items.Single(x => x.Id == id); ctx.Items.Remove(item); ctx.SaveChanges(); } return RedirectToAction("Index"); } catch { ModelState.AddModelError("", "error"); return View(); } }
public virtual ActionResult Edit(ItemEditStartVM vm) { try { using (var ctx = new Desert.DAL.Context()) { Item item = ctx.Items.Include("Type") .Single(x => x.Id == vm.Item.Id); ItemType type = ctx.ItemTypes .Single(x => x.Id == vm.Item.TypeId); Mapper.Map<ItemEditVM, Item>(vm.Item, item); item.Type = type; ctx.SaveChanges(); } return RedirectToAction(ActionNames.Index); } catch (Exception ex) { this.ModelState.AddModelError("", "error"); vm.Types = GetItemTypesSelectList(vm.Item.TypeId); return View(vm); } }