示例#1
0
        public PartialViewResult UpdatePoll(UpdatePollViewModel updatePollViewModel)
        {
            try
            {
                var loggedOnReadOnlyUser = User.GetMembershipUser(MembershipService);

                // Fist need to check this user hasn't voted already and is trying to fudge the system
                if (!_pollVoteService.HasUserVotedAlready(updatePollViewModel.AnswerId, loggedOnReadOnlyUser.Id))
                {
                    var loggedOnUser = MembershipService.GetUser(loggedOnReadOnlyUser.Id);

                    // Get the answer
                    var pollAnswer = _pollAnswerService.Get(updatePollViewModel.AnswerId);

                    // create a new vote
                    var pollVote = new PollVote {
                        PollAnswer = pollAnswer, User = loggedOnUser
                    };

                    // Add it
                    _pollVoteService.Add(pollVote);

                    // Update the context so the changes are reflected in the viewmodel below
                    Context.SaveChanges();
                }

                // Create the view model and get ready return the poll partial view
                var poll         = _pollService.Get(updatePollViewModel.PollId);
                var votes        = poll.PollAnswers.SelectMany(x => x.PollVotes).ToList();
                var alreadyVoted = votes.Count(x => x.User.Id == loggedOnReadOnlyUser.Id) > 0;
                var viewModel    = new PollViewModel
                {
                    Poll                = poll,
                    TotalVotesInPoll    = votes.Count(),
                    UserHasAlreadyVoted = alreadyVoted
                };

                // Commit the transaction
                Context.SaveChanges();

                return(PartialView("_Poll", viewModel));
            }
            catch (Exception ex)
            {
                Context.RollBack();
                LoggingService.Error(ex);
                throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage"));
            }
        }