public ActionResult Details(int id) { if (id == 0) { // No id specified, return to home page. return this.RedirectToAction("Index", "Home"); } using (var ctx = new LunchContext()) { var dictatorship = ctx.Dictatorships.SingleOrDefault(d => d.Id == id); if (dictatorship == null) { // Dictatorship not found! Panic! // Todo: Handle this with some kind of "dictatorship not found" error message return this.RedirectToAction("Index", "Home"); } var model = new DictatorshipViewModel { Id = dictatorship.Id, Name = dictatorship.Name, ImageUrl = dictatorship.ImageUrl }; return this.View(model); } }
public ActionResult Create(DictatorshipViewModel viewModel) { if (!ModelState.IsValid) { return this.View(viewModel); } using (var ctx = new LunchContext()) { var user = ctx.Users.SingleOrDefault(u => u.EmailAddress == User.Identity.Name); var dictatorship = new Dictatorship { Name = viewModel.Name, ImageUrl = viewModel.ImageUrl, Users = new Collection<User> { user } }; ctx.Dictatorships.Add(dictatorship); ctx.SaveChanges(); } this.TempData["Message"] = WebCommon.DictatorshipAdded; return this.View(new DictatorshipViewModel()); }