public async Task <IActionResult> Vote(int pollId, bool showResults = false) { var poll = _polls.GetPollById(pollId); var currentUser = await _userManager.GetUserAsync(User); var model = new PollVoteViewModel { PollId = poll.PollId, Question = poll.Question, Answers = poll.Answers.ToList() }; if (showResults == true) { return(View("~/Features/Polls/VoteResults.cshtml", model)); } else { if (_votes.CheckIfUserAlreadyVoted(pollId, currentUser)) { ModelState.AddModelError("", "You have already voted in this poll."); } return(View("~/Features/Polls/VoteForm.cshtml", model)); } }
public async Task <IActionResult> Vote(int pollId, int?answer) { var poll = _polls.GetPollById(pollId); // lets me include reference to poll & answer in newVote var currentUser = await _userManager.GetUserAsync(User); if (answer == null) { ModelState.AddModelError("", "You have to choose an answer."); } if (_votes.CheckIfUserAlreadyVoted(pollId, currentUser)) { ModelState.AddModelError("", "You have already voted in this poll."); } if (ModelState.IsValid) { Vote newVote = new Vote { User = currentUser, PollId = pollId, AnswerId = answer.Value }; _votes.AddVote(newVote); return(RedirectToAction(nameof(Vote), new { pollid = pollId, showResults = true })); } var model = new PollVoteViewModel { PollId = poll.PollId, Question = poll.Question, Answers = poll.Answers.ToList() }; return(View("~/Features/Polls/VoteForm.cshtml", model)); }