public ActionResult Index([Bind(Include = "Question, Answers")] CreatePollViewModel cpvm) { if(ModelState.IsValid) { Question newQuestion = new Question(); newQuestion.QuestionText = cpvm.Question; Random rnd = new Random(); newQuestion.ShortURL = generateRandom(6,rnd); newQuestion.Password = generateRandom(6,rnd); newQuestion.CreatedDateTime = DateTime.Now; db.Questions.Add(newQuestion); db.SaveChanges(); //int id = newQuestion.Id; Answer[] newAnswer = new Answer[cpvm.Answers.Length]; for(int ndx = 0; ndx<cpvm.Answers.Length; ndx++) { newAnswer[ndx] = new Answer(); newAnswer[ndx].QuestionID = newQuestion.Id; 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 }); } return View(); }
public ActionResult Poll(string shortURL, int Answer) { Question q = new Question(); q = db.Questions.First(x => x.ShortURL == shortURL); Answer a = new Answer(); a = db.Answers.First(x => x.Id == Answer); a.Count = a.Count + 1; db.SaveChanges(); return RedirectToAction("PollResults", new { shortUrl = q.ShortURL }); }