public ActionResult Index(VotingViewModel votes)
        {
            //Not Working To Capture Only Checked Box
            //if (votes.voteResults.Where(x => x.IsSelected == true) != null)
            //    votes.newCheckedBoxID = votes.voteResults.Where(x => x.IsSelected == true).Select(x => x.ID).First();
            //Build Internal DB Object
            Vote vote = new Vote();
            vote.SnackID = votes.newCheckedBoxID;
            vote.VotedOn = votes.VotedOn;

            //Save TO Internal DB On Success
            if (ModelState.IsValid)
            {
                db.Votes.Add(vote);
                db.SaveChanges();
            }
            //Get Cookie Info IF Applicable
            HttpCookie myCookie = HttpContext.Request.Cookies["votes"];
            votes.votedCnt = Convert.ToInt32(myCookie.Value) + 1;

            //Save Cookie - incremented votedCnt
            var voteCookie = new HttpCookie("votes", votes.votedCnt.ToString());
            DateTime begNextMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1);
            voteCookie.Expires = begNextMonth;
            HttpContext.Response.Cookies.Add(voteCookie);

            // Over-Voting Handled Without A Message
            return RedirectToAction("Index");
        }
Exemplo n.º 2
0
 public ActionResult Vote(string Id)
 {
     if (ModelState.IsValid)
     {
         Vote v = new Vote();
         v.SnackId = Convert.ToInt32(Id);
         v.VotedOn = DateTime.Now;
         //Add vote to database
         try {
             db.Votes.Add(v);
             db.SaveChanges();
         }
         catch(Exception ex)
         {
         }
         var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Home");
         return Json(new { Url = redirectUrl });
     }
     return View();
 }