1.今日任务
题目管理及选项管理
在题库管理中 点击查看详情 可查看该题库下的所有题目信息
控制器代码
1 using Exam.BLL; 2 using Exam.Model; 3 using Exam.UI.Filter; 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Web; 8 using System.Web.Mvc; 9 10 namespace Exam.UI.Controllers 11 { 12 [StudentFilter] 13 public class QuestionController : Controller 14 { 15 public ActionResult Index(int id = 0, int page = 1) 16 { 17 ViewBag.ID = id; 18 var list = QuestionService.GetList(id, page); 19 return View(list); 20 } 21 22 // GET: Question 23 [HttpPost] 24 public ActionResult Index(string questionname = "", int page = 1) 25 { 26 var list = QuestionService.GetList(questionname, page); 27 return View(list); 28 } 29 ///30 /// 删除题目 会将挂题目的选项删除 31 /// 32 /// 33 /// 34 public ActionResult DeleteQuestion(int id) 35 { 36 try 37 { 38 int res = QuestionService.Delete(id); 39 } 40 catch (Exception ex) 41 { 42 return Json(new { msg = "删除失败" + ex, success = false }); 43 44 } 45 return Json(new { msg = "删除成功", success = true }); 46 } 47 48 public ActionResult Edit(int questionid) 49 { 50 //获取试题信息 51 var data = QuestionService.GetdataByID(questionid); 52 //获取题库信息 53 ViewBag.Lib = LibraryService.GetAll(); 54 //获取选项信息 55 ViewBag.option = QuestionOptionsService.GetOptions(questionid); 56 57 return View(data); 58 } 59 [HttpPost] 60 public ActionResult Edit(int questionid, int score, string questionparse, string questionanswer, int libraryid, string questiondescribe) 61 { 62 try 63 { 64 Exam_Question question = new Exam_Question 65 { 66 LibraryID = libraryid, 67 QuestionAnswer = questionanswer, 68 QuestionDescribe = questiondescribe, 69 QuestionID = questionid, 70 QuestionParse = questionparse, 71 Score = score 72 }; 73 QuestionService.Update(question); 74 } 75 catch (Exception ex) 76 { 77 78 return Json(new { msg = "修改失败" + ex, success = false }); 79 } 80 81 return Json(new { msg = "修改成功", success = true }); 82 83 } 84 } 85 }
Service层对应代码;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Exam.Model; 6 using Exam.DAL; 7 using System.Threading.Tasks; 8 using PagedList; 9 10 namespace Exam.BLL 11 { 12 public class QuestionService 13 { 14 ///15 /// 试题列表 16 /// 17 /// 18 /// 19 20 public static IPagedList GetList(string questionname, int page) 21 { 22 using (ExamSysDBContext db = new ExamSysDBContext()) 23 { 24 int pagesize = 10; 25 IPagedList list = db.Exam_Question.Where(x => x.QuestionDescribe.Contains(questionname)).OrderBy(x => x.QuestionID).ToPagedList(page, pagesize); 26 return list; 27 } 28 29 } 30 public static IPagedList GetList(int id, int page) 31 { 32 using (ExamSysDBContext db = new ExamSysDBContext()) 33 { 34 int pagesize = 10; 35 IPagedList list; 36 if (id != 0) 37 { 38 list = db.Exam_Question.Where(x => x.LibraryID == id).OrderBy(x => x.QuestionID).ToPagedList(page, pagesize); 39 } 40 else 41 { 42 list = db.Exam_Question.OrderBy(x => x.QuestionID).ToPagedList(page, pagesize); 43 } 44 return list; 45 } 46 47 } 48 public static Exam_Question GetdataByID(int id) 49 { 50 51 ExamSysDBContext db = new ExamSysDBContext(); 52 53 return db.Exam_Question.Where(x => x.QuestionID == id).FirstOrDefault(); 54 55 56 } 57 /// 58 /// 添加试题 59 /// 60 /// 61 /// 62 public static int Add(Exam_Question question) 63 { 64 using (ExamSysDBContext db = new ExamSysDBContext()) 65 { 66 db.Exam_Question.Add(question); 67 db.SaveChanges(); 68 return question.QuestionID; 69 } 70 71 } 72 /// 73 /// 更新试题信息 74 /// 75 /// 76 /// 77 public static int Update(Exam_Question question) 78 { 79 using (ExamSysDBContext db = new ExamSysDBContext()) 80 { 81 var data = db.Exam_Question.Where(x => x.QuestionID == question.QuestionID).FirstOrDefault(); 82 data.QuestionAnswer = question.QuestionAnswer; 83 data.Score = question.Score; 84 data.QuestionParse = question.QuestionParse; 85 data.QuestionDescribe = question.QuestionDescribe; 86 return db.SaveChanges(); 87 } 88 89 } 90 91 public static int GetCountByLibraryID(int id) 92 { 93 using (ExamSysDBContext db = new ExamSysDBContext()) 94 { 95 //删除当前题目下所有选项 96 var list = db.Exam_Question.Where(x => x.LibraryID == id); 97 if(list==null) 98 { 99 return 0; 100 } 101 else 102 { 103 return list.Count(); 104 } 105 } 106 } 107 public static int Delete(int id) 108 { 109 110 using (ExamSysDBContext db = new ExamSysDBContext()) 111 { 112 //删除当前题目下所有选项 113 var list = db.Exam_QuestionOptions.Where(x => x.QuestionID == id).ToList(); 114 db.Exam_QuestionOptions.RemoveRange(list); 115 //删除题目 116 var data = db.Exam_Question.Where(x => x.QuestionID == id).FirstOrDefault(); 117 db.Exam_Question.Remove(data); 118 return db.SaveChanges(); 119 } 120 121 } 122 /// 123 /// 获得题目的分值 124 /// 125 /// 126 /// 127 public static int GetScore(int questionid) 128 { 129 using (ExamSysDBContext db = new ExamSysDBContext()) 130 { 131 var data = db.Exam_Question.Where(x => x.QuestionID == questionid).FirstOrDefault(); 132 return data.Score; 133 } 134 } 135 136 } 137 }
选项管理
在题目列表内点击查看详情即可查看改题目下的选项信息:
可对选项进行修改或者删除操作
控制器对应代码:
1 using Exam.BLL; 2 using Exam.Model; 3 using Exam.UI.Filter; 4 using System; 5 using System.Collections.Generic; 6 using System.Linq; 7 using System.Web; 8 using System.Web.Mvc; 9 10 namespace Exam.UI.Controllers 11 { 12 [StudentFilter] 13 public class QuestionOptionController : Controller 14 { 15 // GET: QuestionOption 16 public int questiuonid = 0; 17 public ActionResult Index(int id, int page = 1) 18 { 19 questiuonid = id; 20 var list = QuestionOptionsService.GetList(id, page); 21 return View(list); 22 } 23 24 public ActionResult Edit(int optionid) 25 { 26 ViewBag.Questionid = questiuonid; 27 ViewBag.Optionid = optionid; 28 var data = QuestionOptionsService.GetOption(optionid); 29 30 return View(data); 31 } 32 [HttpPost] 33 public ActionResult Edit(int id, string optioncode, string optiondescribe) 34 { 35 try 36 { 37 Exam_QuestionOptions option = new Exam_QuestionOptions 38 { 39 OptionCode = optioncode, 40 OptionDescribe = optiondescribe, 41 OptionID = id 42 }; 43 QuestionOptionsService.Update(option); 44 } 45 catch (Exception ex) 46 { 47 48 return Json(new { msg = "修改失败" + ex, success = false }); 49 } 50 return Json(new { msg = "修改成功", success = false }); 51 } 52 53 ///54 /// 删除题目选项 55 /// 56 /// 57 /// 58 public ActionResult Delete(int id) 59 { 60 try 61 { 62 QuestionOptionsService.Delete(id); 63 } 64 catch (Exception ex) 65 { 66 67 return Json(new { msg = "删除失败" + ex, success = false }); 68 } 69 return Json(new { msg = "删除成功", success = true }); 70 } 71 } 72 }
Service对应方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Exam.DAL; using Exam.Model; using System.Threading.Tasks; using System.Data.Entity; using PagedList; using System.Security.Cryptography.X509Certificates; namespace Exam.BLL { public class QuestionOptionsService { public static IPagedList GetList(int questionid, int page) { using (ExamSysDBContext db = new ExamSysDBContext()) { int pagesize = 10; IPagedList list; if (questionid != 0) { list = db.Exam_QuestionOptions.Where(x => x.QuestionID == questionid).OrderBy(x => x.OptionID).ToPagedList(page, pagesize); } else { list = db.Exam_QuestionOptions.OrderBy(x => x.OptionID).ToPagedList(page, pagesize); } return list; } } ////// 通过选项值 和题目编号获取正确选项编号 /// /// /// /// public static string GetOptionID(string optioncode, int questionID) { using (ExamSysDBContext db = new ExamSysDBContext()) { var data = db.Exam_QuestionOptions.Where(X => optioncode.Contains(X.OptionCode) && X.QuestionID == questionID).ToList(); string Temp = ""; string temp2 = ""; foreach (var item in data) { Temp += item.OptionID + ","; } if (Temp.Length>0) { temp2 = Temp.Remove(Temp.Length - 1, 1); } return temp2; } } /// /// 批量添加试题 /// /// public static void AddOptions(List lists) { using (ExamSysDBContext db = new ExamSysDBContext()) { db.Exam_QuestionOptions.AddRange(lists); db.SaveChanges(); } } /// /// 通过题目编号查询 下面的选项 /// /// /// public static List GetOptions(int questionid) { using (ExamSysDBContext db = new ExamSysDBContext()) { return db.Exam_QuestionOptions.Where(x => x.QuestionID == questionid).ToList(); } } /// /// 根据选项ID获取 /// /// /// public static Exam_QuestionOptions GetOption(int optionid) { using (ExamSysDBContext db = new ExamSysDBContext()) { return db.Exam_QuestionOptions.Where(x => x.OptionID == optionid).FirstOrDefault(); } } /// /// 更新选项 /// /// public static int Update(Exam_QuestionOptions option) { using (ExamSysDBContext db = new ExamSysDBContext()) { var data = db.Exam_QuestionOptions.Where(x => x.OptionID == option.OptionID).FirstOrDefault(); data.OptionDescribe = option.OptionDescribe; data.OptionCode = option.OptionCode; data.UpdateTime = DateTime.Now; return db.SaveChanges(); } } public static int Delete(int id) { using (ExamSysDBContext db = new ExamSysDBContext()) { var data = db.Exam_QuestionOptions.Where(x => x.OptionID == id).FirstOrDefault(); db.Exam_QuestionOptions.Remove(data); return db.SaveChanges(); } } } }
修改选项信息页面效果
3.遇到问题
无
4.解决方案
无