public ActionResult AdminPoll(string password) { PollViewModel pvm = new PollViewModel(); Question q = new Question(); q = db.Questions.First(x => x.Password == password); pvm.Question = q; pvm.Answers = db.Answers.Where(x => x.QuestionID == q.QuestionID).ToArray(); return View(pvm); }
public ActionResult PollResults(string shortURL) { PollViewModel pvm = new PollViewModel(); Question q = new Question(); q = db.Questions.First(x => x.ShortURL == shortURL); pvm.Question = q; pvm.Answers = db.Answers.Where(x => x.QuestionID == q.QuestionID).ToArray(); return View(pvm); }
public ActionResult Index([Bind(Include = "Question, Answers, ShortURL")] CreatePollViewModel cpvm) { var currUserID = -1; if (User.Identity.IsAuthenticated) { currUserID = db.Users.First(x => x.Email == User.Identity.Name).UserID; } if (db.Questions.Any(q => q.ShortURL == cpvm.ShortURL && q.CreatedByUserID != currUserID)) { ModelState.AddModelError("ShortURL", "ShortURL in use..Maybe you forgot to login?"); } if (ModelState.IsValid) { Question newQuestion = new Question(); newQuestion.QuestionText = cpvm.Question.ToString().Replace(System.Environment.NewLine, "<br />"); Random rnd = new Random(); if(cpvm.ShortURL == null) { newQuestion.ShortURL = generateRandom(6, rnd); } else { newQuestion.ShortURL = cpvm.ShortURL; } newQuestion.Password = generateRandom(6, rnd); newQuestion.CreatedDateTime = DateTime.Now; if(User.Identity.IsAuthenticated) { User u = db.Users.First(x => x.Email == User.Identity.Name); newQuestion.CreatedByUserID = u.UserID; } newQuestion.Active = true; db.Questions.Add(newQuestion); db.SaveChanges(); //int id = newQuestion.Id; cpvm.Answers = cpvm.Answers.Where(x => !string.IsNullOrEmpty(x.Value)).ToArray(); Answer[] newAnswer = new Answer[cpvm.Answers.Length]; for (int ndx = 0; ndx < cpvm.Answers.Length; ndx++) { newAnswer[ndx] = new Answer(); newAnswer[ndx].QuestionID = newQuestion.QuestionID; newAnswer[ndx].AnswerText = cpvm.Answers[ndx].Value; db.Answers.Add(newAnswer[ndx]); } db.SaveChanges(); //PollViewModel pvm = new PollViewModel(); //pvm.Question = cpvm.Question; //pvm.Answers = cpvm.Answers.Select(x => x.Value).ToArray(); //return RedirectToAction("AdminPoll", new { password = newQuestion.Password }); if(User.Identity.IsAuthenticated) { return RedirectToAction("ManagePolls"); } else { return RedirectToAction("AdminPoll", new { password = newQuestion.Password }); } } return View(); }