public RedirectToRouteResult SaveQuiz()
        {
            var queList = (List <Question>)Session["Questionlist"];

            QuestionDM queDM    = new QuestionDM();
            QuizBank   quizBank = new QuizBank();

            Quiz q = new Quiz {
                ID           = quizBank.GetMaxID() + 1,
                Title        = (string)Session["Title"],
                Image        = (string)Session["Image"],
                Creator      = (string)Session["Creator"],
                Desp         = (string)Session["Desp"],
                QuestionList = new List <Question>()
            };


            if (string.IsNullOrEmpty(q.Image))
            {
                q.Image = "default.png";
            }
            int maxID = queDM.GetMaxID();

            if (queList != null)
            {
                foreach (var item in queList)
                {
                    item.ID     = ++maxID;
                    item.QuizID = q.ID;
                    q.QuestionList.Add(item);
                }
            }
            quizBank.AddNewQuiz(q);

            Session["Title"]        = null;
            Session["Image"]        = null;
            Session["QuestionList"] = null;

            return(RedirectToAction("index", "MyQuiz"));
        }
Exemple #2
0
        public ActionResult AddQuestionToQuiz(HttpPostedFileBase file, FormCollection frm)
        {
            QuestionDM queDM = new QuestionDM();

            int quizID = int.Parse(frm["quizID"]);

            string c1    = frm["TxtC1"];
            string c2    = frm["TxtC2"];
            string c3    = frm["TxtC3"];
            string c4    = frm["TxtC4"];
            string ans   = frm["TxtAns"];
            string image = "";

            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    string path = Path.Combine(Server.MapPath("~/resources/images/QuestionImages"),
                                               Path.GetFileName(file.FileName));
                    file.SaveAs(path);

                    // WebImage belong to WebHelper class which supports the crop, flip, watermark operation etc.
                    WebImage img = new WebImage(file.InputStream);
                    if (img.Width > 1200)
                    {
                        img.Resize(1200, 600);
                    }
                    img.Save(path);

                    image = file.FileName;
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            }
            else if (file == null)
            {
                image = "default.png";
            }

            Question q = new Question
            {
                QuizID  = quizID,
                Content = frm["TxtContent"],
                AnsA    = c1,
                AnsB    = c2,
                AnsC    = c3,
                AnsD    = c4,
                Time    = int.Parse(frm["TxtTime"]),
                Answer  = ans,
                Image   = image
            };



            q.ID = queDM.GetMaxID() + 1;

            queDM.AddQuestion(q);

            return(View("EditQuiz", new QuizBank().FindQuizByID(quizID)));
        }