Example #1
0
        public ActionResult EditQuestion(Question question, string btnSubmit)
        {
            List<int> alternativeDeletId=new List<int>();
            if (question.Alternatives[question.Alternatives.Count - 1].AlternativeText == null) question.Alternatives.RemoveAt(question.Alternatives.Count - 1);
            //List<int> number=new List<int>();
            //for (int i=question.Alternatives.Count-1;i>=0;i--)
            //{
            //    if(question.Alternatives[i].AlternativeText==null) number.Add(i);
            //}
            //foreach (var row in number)
            //{
            //    question.Alternatives.RemoveAt(row);
            //}
            if (btnSubmit == "Add Alternative")
            {
                return View(question);
            }
            if (btnSubmit == "Save")
            {
                var quest = _db.Questions.Single(q => q.Id == question.Id);
                quest.Published = question.Published;
                quest.QuestionText = question.QuestionText;
                int max;

                for (int i = question.Alternatives.Count-1; i >=0; i--)
                {
                    if (i < quest.Alternatives.Count)
                    {
                        if (question.Alternatives[i].AlternativeText == null)
                        {
                            int a = quest.Alternatives[i].Id;
                            alternativeDeletId.Add(a);
                            quest.Alternatives.RemoveAt(i);
                        }
                    }
                    else quest.Alternatives.Add(new Alternative(){AlternativeText = question.Alternatives[i].AlternativeText});
                }

                    _db.SaveChanges();

                return RedirectToAction("Edit");
            }
            return View(question);
        }
Example #2
0
        public ActionResult NewQuestion(Question question,string btnSubmit)
        {
            if (question.Alternatives[question.Alternatives.Count - 1].AlternativeText == null) question.Alternatives.RemoveAt(question.Alternatives.Count - 1);
            if (btnSubmit == "Add Alternative")
            {
                return View(question);
            }
            if (btnSubmit == "Create")
            {
                question.UserId = (int)WebSecurity.CurrentUserId;
                _db.Questions.Add(question);

                _db.SaveChanges();

                return RedirectToAction("Edit");
            }
            else return View(question);
        }