public ActionResult CreateVote(CreateNewVoteModel input) { var birtdayPeople = this.birthdayData .GetAllBirthdayPeople(this.User.Identity.GetUserId()) .ToList(); input.BirthdayPeople = new SelectList(birtdayPeople, "Id", "Name"); if (ModelState.IsValid) { var currentUser = this.User.Identity.GetUserId(); if (input.BirthdayPerson == currentUser) { throw new InvalidOperationException("It is allowed to start a new vote for the other people only!"); } var voteExist = this.voteData.GetVote(input.BirthdayPerson, input.Year).FirstOrDefault(); if (voteExist != null) { //throw new InvalidOperationException("There is a vote for this user and year!"); return View("Error"); } var birthdayPerson = this.birthdayData.GetUser(input.BirthdayPerson).FirstOrDefault(); var initiator = this.birthdayData.GetUser(currentUser).FirstOrDefault(); var vote = new Vote { BirthdayPersonId = birthdayPerson.Id, //BirthdayPerson = birthdayPerson, InitiatorId = currentUser, //Initiator = initiator, StartDate = input.StartDate, EndDate = input.EndDate, Year = input.Year }; this.dbData.Votes.Add(vote); this.dbData.Votes.SaveChanges(); return this.RedirectToAction("OpenedVotes"); } return View(input); }
public ActionResult CreateVote() { // http://stackoverflow.com/questions/18448637/how-to-get-current-user-and-how-to-use-user-class-in-mvc5 var birtdayPeople = this.birthdayData .GetAllBirthdayPeople(this.User.Identity.GetUserId()) .ToList(); var model = new CreateNewVoteModel(); model.BirthdayPeople = new SelectList(birtdayPeople, "Id", "Name"); return View(model); }