public ActionResult Edit(int id, CreateUpdateEventViewModel model) { if (ModelState.IsValid) { using (DatabaseContext db = HttpContext.GetOwinContext().Get <DatabaseContext>()) { Event Event = db.Events.Include("Categories").First(e => e.Id == id); if (Event == null) { return(HttpNotFound()); } Event.Name = model.Name; Event.Description = model.Description; Event.Location = model.Location; Event.Categories = model.SelectedCategoryIds != null && model.SelectedCategoryIds.Any() ? db.Categories.Where(c => model.SelectedCategoryIds.Contains(c.Id)).ToList() : new List <Category>(); Event.Datetime = model.Datetime; Event.Limit = model.Limit; Event.Images = model.Images; db.SaveChanges(); Message.Create(Response, "Event was successfully edited."); } } return(RedirectToAction("Detail", "Events", new { id })); }
public ActionResult Create(CreateUpdateEventViewModel model) { if (ModelState.IsValid) { using (DatabaseContext db = HttpContext.GetOwinContext().Get <DatabaseContext>()) { Event @event = new Event { Name = model.Name, Description = model.Description, Location = model.Location, Datetime = model.Datetime, Limit = model.Limit, Categories = model.SelectedCategoryIds != null && model.SelectedCategoryIds.Any() ? db.Categories.Where(c => model.SelectedCategoryIds.Contains(c.Id)).ToList() : new List <Category>(), CreatedAt = DateTime.Now, Host = db.Users.Find(User.Identity.GetUserId()), Images = model.Images }; db.Events.Add(@event); db.SaveChanges(); Message.Create(Response, "Event was successfully created."); return(RedirectToAction("Detail", "Events", new { id = @event.Id })); } } else { DatabaseContext db = HttpContext.GetOwinContext().Get <DatabaseContext>(); return(View(new CreateUpdateEventViewModel { Datetime = DateTime.Now, Categories = db.Categories.ToList() })); } return(View()); }
public ActionResult Edit(int id) { using (DatabaseContext db = HttpContext.GetOwinContext().Get <DatabaseContext>()) { Event @event = db.Events.Find(id); if (@event.Host.Id != User.Identity.GetUserId()) { return(RedirectToAction("Index", "Home")); } CreateUpdateEventViewModel CreateUpdateEventViewModel = new CreateUpdateEventViewModel() { Id = @event.Id, Name = @event.Name, Description = @event.Description, Location = @event.Location, Datetime = @event.Datetime, Limit = @event.Limit, Categories = db.Categories.ToList(), SelectedCategoryIds = @event.Categories.ToList().Select(c => c.Id), Images = @event.Images }; return(View(CreateUpdateEventViewModel)); } }