예제 #1
0
        public ActionResult Multiple(long pollId, [FromForm] long[] choices)
        {
            if (choices.Length == 0)
            {
                TempData["ErrorMessage"] = "List of choices is empty!";
            }
            else
            {
                // assert all choices belongs to selected poll
                // first: select all known choices from poll #pollId
                long [] knownChoices =
                    _db.PollChoices.Where(ch => ch.PollId == pollId)
                    .Select(ch => ch.Id)
                    .ToArray();

                // lookup for any unknown choices among param choices
                if (choices.Except(knownChoices).Count() > 0)
                {
                    TempData["ErrorMessage"] = "Wrong choices! (not belongs to given poll)";
                }
                else
                {
                    // this ip voted for this poll?
                    if (_db.PollVotes2.Count(v => v.PollId == pollId && v.IpAddress == _ip) > 0)
                    {
                        TempData["ErrorMessage"] = "You can't vote more then once on this poll!";
                    }
                    else
                    {
                        // OK to save vote
                        _db.PollVotes2.Add(new PollVotes2()
                        {
                            Id          = _db.NextPollVote2Id(),
                            PollId      = pollId,
                            IpAddress   = _ip,
                            UserAgent   = _ua,
                            PollChoices = string.Join(",", choices.Select(c => c.ToString()).ToArray())
                        });
                    }
                }
            }

            return(RedirectToAction("Show", "Polls"));
        }