Example #1
0
        public ActionResult Index(Poll poll)
        {
            //TODO enhancement check if some options arent same
            ViewData["isCorrect"] = true;

            if (String.IsNullOrWhiteSpace(poll.QuestionText) || poll.QuestionText == "enter your poll question here...")
            {
                ModelState.AddModelError("", "Please enter you poll question.");
                ViewData["isCorrect"] = false;

                return View(poll);
            }

            if (ModelState.IsValid)
            {
                List<Option> filtredOptions = new List<Option>();
                foreach (var option in poll.Options)
                {
                    if (!String.IsNullOrWhiteSpace(option.OptionText) && option.OptionText != "enter option here...")
                    {
                        option.WasChecked = false;
                        option.PollId = poll.PollId;
                        filtredOptions.Add(option);
                    }
                }
                if (filtredOptions.Count < 2)
                {
                    ModelState.AddModelError("", "Poll must contain atleast two options to chose.");
                    ViewData["isCorrect"] = false;
                    return View(poll);
                }

                poll.Options = filtredOptions;
                db.Polls.Add(poll);
                db.SaveChanges();

                return RedirectToAction("Vote", new { id = poll.PollId });

            }

            return View();
        }
Example #2
0
        public ActionResult Vote(Poll poll)
        {
            //TODO

            /*
            if (db.Votes.Where(x => x.PollId == poll.PollId && x.VoterIp == System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]).Count() > 0)
            {
                    ModelState.AddModelError("", "You have already voted.");
                    return View(poll);
            }
            */

            if (poll.MultipleChoices)
            {
                foreach (var option in poll.Options)
                {
                    if (option.WasChecked)
                    {
                        Vote vote = new Vote();
                        vote.VoterIp = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                        // or HttpContext.Current.Request.UserHostAddress;
                        vote.PollId = option.PollId;
                        vote.OptionId = option.OptionId;
                        db.Votes.Add(vote);

                        //TODO lock for counting
                        Option optionToUdt = db.Options.Find(vote.OptionId);
                        optionToUdt.NumVotes += 1;
                        db.SaveChanges();

                    }
                }
            }
            else
            {
                Vote vote = new Vote();
                vote.VoterIp = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                // or HttpContext.Current.Request.UserHostAddress;
                vote.PollId = poll.PollId;
                vote.OptionId = (int)poll.SelectedItem;
                db.Votes.Add(vote);

                //TODO lock for counting
                Option optionToUdt = db.Options.Find(vote.OptionId);
                optionToUdt.NumVotes += 1;
                db.SaveChanges();
            }

            return RedirectToAction("Result", new { id = poll.PollId });
        }