public ActionResult Create(FormCollection collection) { var model = new Exam(); TryUpdateModel(model, collection.ToValueProvider()); if (!ModelState.IsValid) { return View(model); } using (var session = new SessionFactory().OpenSession()) { session.BeginTransaction(); model.CreatedAt = DateTime.Now; model.CreatedBy = CurrentAccountNo; ViewData.Model = model; if (session.Create(model) && CreatQuestions(model, session)) { session.Commit(); FlashSuccess("创建记录成功!"); return Close(); } session.Rollback(); FlashFailure("创建记录失败!"); return View(); } }
private bool CreatQuestions(Exam model, Session session) { var count = 1; if (model.QuestionCount1.HasValue && model.QuestionCount1 != 0) { var list = new List<Question>(); if (string.IsNullOrEmpty(model.Answer1)) { ModelState.AddModelError("Answer1", "答案不能为空!"); return false; } var answerList = model.Answer1.Split(','); if (answerList.Count() != model.QuestionCount1) { ModelState.AddModelError("Answer1", "答案个数不匹配!"); return false; } if (model.Value1 == null) { ModelState.AddModelError("Value1", "分值不能为空!"); return false; } if (model.OptionCount1 == null) { ModelState.AddModelError("OptionCount1", "选项个数不能为空!"); return false; } for (var i = 0; i < model.QuestionCount1; i++) { list.Add(new Question { No = count++, ExamId = model.Id, CreatedAt = DateTime.Now, CreatedBy = "SYSTEM", Type = QuestionType.单选, OptionCount = model.OptionCount1.Value, Value = model.Value1.Value, RightAnswer = answerList[i] }); } if (!session.Create(list)) return false; } if (model.QuestionCount2.HasValue&&model.QuestionCount2 != 0) { var list = new List<Question>(); if (string.IsNullOrEmpty(model.Answer2)) { ModelState.AddModelError("Answer2", "答案不能为空!"); return false; } if (model.Value2 == null) { ModelState.AddModelError("Value2", "分值不能为空!"); return false; } if (model.OptionCount2 == null) { ModelState.AddModelError("OptionCount2", "选项个数不能为空!"); return false; } var answerList = model.Answer2.Split(','); if (answerList.Count() != model.QuestionCount2) { ModelState.AddModelError("Answer2", "答案个数不匹配!"); return false; } for (var i = 0; i < model.QuestionCount2; i++) { list.Add(new Question { No = count++, ExamId = model.Id, CreatedAt = DateTime.Now, CreatedBy = "SYSTEM", Type = QuestionType.多选, OptionCount = model.OptionCount2.Value, Value = model.Value2.Value, RightAnswer = answerList[i] }); } if (!session.Create(list)) return false; } if (model.QuestionCount3.HasValue && model.QuestionCount3 != 0) { var list = new List<Question>(); if (string.IsNullOrEmpty(model.Answer3)) { ModelState.AddModelError("Answer3", "答案不能为空!"); return false; } if (model.Value3 == null) { ModelState.AddModelError("Value3", "分值不能为空!"); return false; } if (model.OptionCount3 == null) { ModelState.AddModelError("OptionCount3", "选项个数不能为空!"); return false; } var answerList = model.Answer3.Split(','); if (answerList.Count() != model.QuestionCount3) { ModelState.AddModelError("Answer3", "答案个数不匹配!"); return false; } for (var i = 0; i < model.QuestionCount3; i++) { list.Add(new Question { No = count++, ExamId = model.Id, CreatedAt = DateTime.Now, CreatedBy = "SYSTEM", Type = QuestionType.不定项选择, OptionCount = model.OptionCount3.Value, Value = model.Value3.Value, RightAnswer = answerList[i] }); } if (!session.Create(list)) return false; } if (model.QuestionCount4.HasValue && model.QuestionCount4 != 0) { var list = new List<Question>(); if (string.IsNullOrEmpty(model.Answer4)) { ModelState.AddModelError("Answer4", "答案不能为空!"); return false; } if (string.IsNullOrEmpty(model.Value4)) { ModelState.AddModelError("Value4", "分值不能为空!"); return false; } var answerList = model.Answer4.Split('\n'); if (answerList.Count() != model.QuestionCount4) { ModelState.AddModelError("Answer4", "答案个数不匹配!"); return false; } var valueList = model.Value4.Split(','); if (valueList.Count() != model.QuestionCount4) { ModelState.AddModelError("Value4", "分值个数不匹配!"); return false; } for (var i = 0; i < model.QuestionCount4; i++) { var value = valueList[i].TryToInt(); if (value == null) { ModelState.AddModelError("Value4", "分值格式有误!"); return false; } list.Add(new Question { No = count++, ExamId = model.Id, CreatedAt = DateTime.Now, CreatedBy = "SYSTEM", Type = QuestionType.主观题, Value = value.Value, RightAnswer = answerList[i] }); } if (!session.Create(list)) return false; } return true; }