Exemplo n.º 1
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)));
        }