public ActionResult Index(CreateGroupViewModel model) { var chosenPlace = this.Db.PlaceRepository.All().FirstOrDefault(x => x.Name == model.PlacePlaceholder); var userId = User.Identity.GetUserId(); var user = this.Db.UsersRepository.All().FirstOrDefault(x => x.Id == userId); var newGroup = new HungryGroup { Creator = user, Place = chosenPlace, StartingTime = model.StartingTime, EatTime = chosenPlace.EatTime }; this.Db.HungryGroupRepository.Add(newGroup); this.Db.SaveChanges(); return View(); }
public ActionResult Create(HungryGroupViewModel hungrygroup) { if (ModelState.IsValid) { hungrygroup.Creator = this.Db.UsersRepository.All().FirstOrDefault(x => x.UserName == User.Identity.Name); hungrygroup.Place = this.Db.PlaceRepository.All().FirstOrDefault(x => x.Id == hungrygroup.PlaceId); HungryGroup newGroup = new HungryGroup() { Creator = hungrygroup.Creator, EatTime = hungrygroup.EatTime, HungryUsers = hungrygroup.HungryUsers, Id = hungrygroup.Id, Place = hungrygroup.Place, StartingTime = hungrygroup.StartingTime }; this.Db.HungryGroupRepository.Add(newGroup); this.Db.SaveChanges(); return RedirectToAction("Index"); } return View(hungrygroup); }
public ActionResult Edit(HungryGroupViewModel hungrygroup) { var place = db.Places.FirstOrDefault(x => x.Id == hungrygroup.Id); HungryGroup editedHungryGroup = new HungryGroup() { Creator = hungrygroup.Creator, EatTime = hungrygroup.EatTime, HungryUsers = hungrygroup.HungryUsers, Id = hungrygroup.Id, Place = place, StartingTime = hungrygroup.StartingTime }; if (ModelState.IsValid) { db.Entry(editedHungryGroup).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(hungrygroup); }