/// <summary>
 /// Create a new TN_Answer object.
 /// </summary>
 /// <param name="id">Initial value of the ID property.</param>
 /// <param name="questionID">Initial value of the QuestionID property.</param>
 /// <param name="content">Initial value of the Content property.</param>
 /// <param name="isCorrect">Initial value of the IsCorrect property.</param>
 public static TN_Answer CreateTN_Answer(global::System.Int32 id, global::System.Int32 questionID, global::System.String content, global::System.Boolean isCorrect)
 {
     TN_Answer tN_Answer = new TN_Answer();
     tN_Answer.ID = id;
     tN_Answer.QuestionID = questionID;
     tN_Answer.Content = content;
     tN_Answer.IsCorrect = isCorrect;
     return tN_Answer;
 }
        public ActionResult CreateQuestionFromExcelFile(int examID, HttpPostedFileBase file)
        {
            try
            {
                if (file != null)
                {
                    var fileUtil = new FileUtil();
                    string folderUpload = @"D:\FileUploads\";
                    if (!fileUtil.CheckDirectoryExists(folderUpload))
                    {
                        fileUtil.CreateDirectory(folderUpload);
                    }
                    string fileName = "nganhangcauhoi" + file.FileName.Substring(file.FileName.LastIndexOf("."));
                    file.SaveAs(folderUpload + fileName);

                    // đọc file excel và lưu vào csdl
                    //Stream streamDictionary = GetResourceFileStream(folderUpload + fileName);
                    using (var document = SpreadsheetDocument.Open(folderUpload + fileName, false))
                    {
                        var workbookPart = document.WorkbookPart;
                        var workbook = workbookPart.Workbook;

                        var sheets = workbook.Descendants<Sheet>();
                        foreach (var sheet in sheets)
                        {
                            if (sheet.SheetId.HasValue && sheet.SheetId.ToString().Equals("1"))
                            {
                                #region Read excel

                                var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);
                                var sharedStringPart = workbookPart.SharedStringTablePart;
                                var values = sharedStringPart.SharedStringTable.Elements<SharedStringItem>().ToArray();

                                var cells = worksheetPart.Worksheet.Descendants<Cell>();
                                var rows = worksheetPart.Worksheet.Descendants<Row>();
                                TN_Question questionTemp = new TN_Question();
                                int countDataNull = 0;
                                bool flag = false;
                                foreach (Row row in rows)
                                {
                                    var question = new TN_Question();
                                    var answer = new TN_Answer();
                                    foreach (Cell c in row.Elements<Cell>())
                                    {
                                        string value = string.Empty;
                                        if ((c.DataType != null) && (c.DataType == CellValues.SharedString))
                                        {
                                            int ssid = int.Parse(c.CellValue.Text);
                                            value = sharedStringPart.SharedStringTable.ChildElements[ssid].InnerText;
                                        }
                                        else if (c.CellValue != null)
                                        {
                                            value = c.CellValue.Text;
                                        }

                                        // xử lý ban đầu
                                        value = value.TrimEnd('%');

                                        if (c.CellReference == "A" + row.RowIndex)
                                        {
                                            if (!string.IsNullOrEmpty(value))
                                            {
                                                question.Content = value;
                                                answer.Content = value;
                                                countDataNull = 0;
                                            }
                                            else
                                            {
                                                countDataNull++;
                                                break;
                                            }
                                        }
                                        if (c.CellReference == "B" + row.RowIndex)
                                        {
                                            if (!string.IsNullOrEmpty(value))
                                            {
                                                answer.IsCorrect = true;
                                            }
                                            else
                                            {
                                                answer.IsCorrect = false;
                                            }
                                        }
                                        if (c.CellReference == "C" + row.RowIndex && !string.IsNullOrEmpty(value))
                                        {
                                            if (!string.IsNullOrEmpty(value))
                                            {
                                                question.Type = value;
                                                flag = true;
                                            }
                                        }
                                    }

                                    if (countDataNull == 0)
                                    {
                                        if (flag)
                                        {
                                            question.IsMultiChoose = false;
                                            db.TN_Question.AddObject(question);

                                            questionTemp = question;

                                            var questionExam = new TN_ExamQuestion()
                                            {
                                                ExamID = examID,
                                                QuestionID = question.ID

                                            };
                                            db.TN_ExamQuestion.AddObject(questionExam);
                                            flag = false;
                                        }
                                        else
                                        {
                                            answer.QuestionID = questionTemp.ID;
                                            db.TN_Answer.AddObject(answer);
                                        }
                                        db.SaveChanges();
                                    }
                                }
                                #endregion
                                break;
                            }
                        }
                    }
                    ViewBag.Success = true;
                }
            }
            catch (Exception ex)
            {

            }
            return View(new List<TN_Exam>());
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the TN_Answer EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToTN_Answer(TN_Answer tN_Answer)
 {
     base.AddObject("TN_Answer", tN_Answer);
 }
        public ActionResult SaveAnswer(int id, FormCollection collection)
        {
            var db = DB.GetContext();
            var question = db.TN_Question.SingleOrDefault(m => m.ID == id);
            if (question != null)
            {
                var rbAnswer = collection["rb" + id.ToString()];
                //Sửa câu trả lời cũ
                foreach (var answer in question.TN_Answer)
                {
                    var content = collection["txt" + answer.ID.ToString()];
                    if (string.IsNullOrEmpty(content))
                    {
                        ModelState.AddModelError("", "Bạn phải nhập vào nội dung câu trả lời");
                        return View(question);
                    }
                    else
                    {
                        var cbAnswer = collection["cb" + answer.ID.ToString()];
                        answer.IsCorrect = (!string.IsNullOrEmpty(cbAnswer) && cbAnswer.Contains("true")) || (!string.IsNullOrEmpty(rbAnswer) && rbAnswer.Contains(answer.Content));
                        answer.Content = content;
                    }
                }

                //Thêm câu trả lời mới
                if (!string.IsNullOrEmpty(collection["txtAddAnswer"]))
                {
                    var answer = new TN_Answer();
                    var cbAnswer = collection["cbAddAnswer"];
                    var txtAnswer = collection["txtAddAnswer"];
                    if (string.IsNullOrEmpty(txtAnswer))
                    {
                        ModelState.AddModelError("", "Bạn phải nhập vào nội dung câu trả lời");
                        return View(question);
                    }
                    else
                    {
                        answer.IsCorrect = (!string.IsNullOrEmpty(cbAnswer) && cbAnswer.Contains("true")) || (!string.IsNullOrEmpty(rbAnswer) && rbAnswer.Contains("AddAnswer"));
                        answer.QuestionID = id;
                        answer.Content = txtAnswer.Trim();

                        question.TN_Answer.Add(answer);
                    }
                }

                db.SaveChanges();
            }

            return RedirectToAction("Edit", new { id = id });
        }