今日任务:
1、机构/校区管理
1.1机构/校区列表
页面效果:
核心代码:
public List<SelectListItem> GetInstitutionForDDL()
{
List<SelectListItem> list = GetAllInstitution().Select(a => new SelectListItem
{
Text = a.i_Name,
Value = a.institution_Id.ToString()
}).ToList();
return list;
}
1.2添加机构/校区信息
页面效果:
核心代码:
public ActionResult AddInstitution()
{
return View();
}
/// <summary>
/// 添加机构/校区--实现功能
/// </summary>
/// <param name="model">机构/校区信息</param>
/// <returns></returns>
[HttpPost]
public ActionResult AddInstitution(AddInstitutionModel model)
{
Institution it = new Institution
{
i_Name = model.i_Name,
city = model.city,
manager_Name = model.manager_Name,
manager_Tel = model.manager_Tel,
status = 1
};
using (OnLineExamDB db = new OnLineExamDB())
{
db.Institution.Add(it);
db.SaveChanges();
return RedirectToAction("Institution");
}
}
1.3修改机构/校区信息
页面效果:
核心代码:
public ActionResult UpdateInstitution(int id)
{
using (OnLineExamDB db = new OnLineExamDB())
{
UpdateInstitutionModel model = db.Institution.Where(a => a.institution_Id == id).Select(a => new UpdateInstitutionModel
{
institution_Id = a.institution_Id,
i_Name = a.i_Name,
city = a.city,
manager_Name = a.manager_Name,
manager_Tel = a.manager_Tel
}).FirstOrDefault();
return View(model);
}
}
/// <summary>
/// 修改机构/校区信息--视图
/// </summary>
/// <param name="model">机构信息</param>
/// <returns></returns>
[HttpPost]
public ActionResult UpdateInstitution(UpdateInstitutionModel model)
{
using (OnLineExamDB db = new OnLineExamDB())
{
Institution it = db.Institution.First(a => a.institution_Id == model.institution_Id);
it.i_Name = model.i_Name;
it.city = model.city;
it.manager_Name = model.manager_Name;
it.manager_Tel = model.manager_Tel;
db.SaveChanges();
return View("InstitutionList");
}
}
1.4删除机构/校区信息
页面效果:
核心代码:
/// <summary>
/// 删除机构/校区信息
/// </summary>
/// <param name="id">主键编号</param>
/// <returns></returns>
[HttpPost]
public ActionResult DelInstitution(int id)
{
using (OnLineExamDB db = new OnLineExamDB())
{
Institution model = db.Institution.First(a => a.institution_Id == id);
model.status = 2;
db.SaveChanges();
return Json("ok");
}
}
2、题库管理
2.1题库列表
页面效果:
核心代码:
public ActionResult QuestionBankList()
{
using (OnLineExamDB db = new OnLineExamDB())
{
List<QuestionBankListModel> list = db.QuestionBank.Where(a=>a.status!=2).Select(a => new QuestionBankListModel
{
rightAnswer = a.rightAnswer,
course_Id = a.course_Id,
course_Name = a.CourseInfo.course_Name,
question_Id = a.question_Id,
question_Option1 = a.question_Option1,
question_Option2 = a.question_Option2,
question_Option3 = a.question_Option3,
question_Option4 = a.question_Option4,
question_Option5 = a.question_Option5,
question_Title = a.question_Title,
Remark = a.Remark
}).ToList();
return View(list);
}
}
2.2添加题库
页面效果:
核心代码:
/// <summary>
/// 添加题库--视图
/// </summary>
/// <returns></returns>
public ActionResult AddQuestionBank()
{
ViewBag.Course = GetCourseInfoForDDL();
return View();
}
/// <summary>
/// 添加题库--实现功能
/// </summary>
/// <param name="model">试题信息</param>
/// <returns></returns>
[HttpPost]
public ActionResult AddQuestionBank(AddQuestionBankModel model)
{
QuestionBank qb = new QuestionBank
{
rightAnswer = model.rightAnswer,
course_Id = model.course_Id,
question_Option1 = model.question_Option1,
question_Option2 = model.question_Option2,
question_Option3 = model.question_Option3,
question_Option4 = model.question_Option4,
question_Option5 = model.question_Option5,
question_Title = model.question_Title,
status=1,
Remark = model.Remark
};
using (OnLineExamDB db = new OnLineExamDB())
{
db.QuestionBank.Add(qb);
db.SaveChanges();
return RedirectToAction("QuestionBankList");
}
}
2.3修改题库
页面效果:
核心代码:
/// <summary>
/// 修改题库信息--视图
/// </summary>
/// <param name="id">主键编号</param>
/// <returns></returns>
public ActionResult UpdateQuestionBank(int id)
{
using (OnLineExamDB db = new OnLineExamDB())
{
UpdateQuestionBank model = db.QuestionBank.Where(a => a.question_Id == id).Select(a => new UpdateQuestionBank
{
rightAnswer=a.rightAnswer,
course_Id=a.course_Id,
question_Id=a.question_Id,
question_Option1=a.question_Option1,
question_Option2=a.question_Option2,
question_Option3=a.question_Option3,
question_Option4=a.question_Option4,
question_Option5=a.question_Option5,
question_Title=a.question_Title,
Remark=a.Remark
}).FirstOrDefault();
ViewBag.Course = GetCourseInfoForDDL();
return View(model);
}
}
/// <summary>
/// 修改题库信息--功能实现
/// </summary>
/// <param name="model">题库信息</param>
/// <returns></returns>
[HttpPost]
public ActionResult UpdateQuestionBank(UpdateQuestionBank model)
{
using (OnLineExamDB db = new OnLineExamDB())
{
QuestionBank qb = db.QuestionBank.First(a => a.question_Id == model.question_Id);
qb.question_Option1 = model.question_Option1;
qb.question_Option2 = model.question_Option2;
qb.question_Option3 = model.question_Option3;
qb.question_Option4 = model.question_Option4;
qb.question_Option5 = model.question_Option5;
qb.question_Title = model.question_Title;
qb.Remark = model.Remark;
qb.rightAnswer = model.rightAnswer;
db.SaveChanges();
return View("QuestionBankList");
}
}
2.4删除题库
页面效果:
核心代码:
[HttpPost]
public ActionResult DelQuestionBank(int id)
{
using (OnLineExamDB db = new OnLineExamDB())
{
QuestionBank model = db.QuestionBank.First(a => a.question_Id == id);
model.status = 2;
db.SaveChanges();
return Json("ok");
}
}
2.5批量导入题库
页面效果:
核心代码:
/// <summary>
/// 批量导入题库-视图
/// </summary>
/// <returns></returns>
public ActionResult ImportQuestionBank()
{
ViewBag.Course = GetCourseInfoForDDL();
return View();
}
[HttpPost]
public ActionResult ImportQuestionBank(HttpPostedFileBase filebase)
{
HttpPostedFileBase file = Request.Files["files"];
string FileName;
string savePath;
if (file == null || file.ContentLength <= 0)
{
ViewBag.error = "文件不能为空";
return View();
}
else
{
string filename = Path.GetFileName(file.FileName);
int filesize = file.ContentLength;//获取上传文件的大小单位为字节byte
string fileEx = System.IO.Path.GetExtension(filename);//获取上传文件的扩展名
string NoFileName = System.IO.Path.GetFileNameWithoutExtension(filename);//获取无扩展名的文件名
int Maxsize = 4000 * 1024;//定义上传文件的最大空间大小为4M
string FileType = ".xls,.xlsx";//定义上传文件的类型字符串
FileName = NoFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx;
if (!FileType.Contains(fileEx))
{
ViewBag.error = "文件类型不对,只能导入xls和xlsx格式的文件";
return View();
}
if (filesize >= Maxsize)
{
ViewBag.error = "上传文件超过4M,不能上传";
return View();
}
string path = AppDomain.CurrentDomain.BaseDirectory + "/Content/uploads/excel/question";
savePath = Path.Combine(path, FileName);
file.SaveAs(savePath);
}
//string result = string.Empty;
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + ";" + "Extended Properties=Excel 8.0";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
try
{
myCommand.Fill(myDataSet, "ExcelInfo");
}
catch (Exception ex)
{
ViewBag.error = ex.Message;
return View();
}
DataTable table = myDataSet.Tables["ExcelInfo"].DefaultView.ToTable();
//引用事务机制,出错时,事物回滚
using (TransactionScope transaction = new TransactionScope())
{
for (int i = 0; i < table.Rows.Count; i++)
{
QuestionBank qb = new QuestionBank();
qb.course_Id=Convert.ToInt32(table.Rows[i][0].ToString());
qb.question_Title= table.Rows[i][1].ToString();
qb.question_Option1= table.Rows[i][2].ToString();
qb.question_Option2 = table.Rows[i][3].ToString();
qb.question_Option3 = table.Rows[i][4].ToString();
qb.question_Option4 = table.Rows[i][5].ToString();
qb.rightAnswer = table.Rows[i][6].ToString();
qb.status = 1;
using (OnLineExamDB db = new OnLineExamDB())
{
db.QuestionBank.Add(qb);
db.SaveChanges();
}
}
transaction.Complete();
}
ViewBag.error = "导入成功";
System.Threading.Thread.Sleep(2000);
return RedirectToAction("Index");
}
/// <summary>
/// 下载题库模板
/// </summary>
/// <returns></returns>
public FileResult GetQuestionBankFile()
{
string path = AppDomain.CurrentDomain.BaseDirectory + "/Content/uploads/excel/question";
string fileName = "题库导入模板.xls";
return File(path + fileName, "text/plain", fileName);
}
3、卷库管理
3.1卷库列表
页面效果:
核心代码:
/// <summary>
/// 卷库列表
/// </summary>
/// <returns></returns>
public ActionResult PaperLibraryList()
{
using (OnLineExamDB db = new OnLineExamDB())
{
List<PaperLibraryListModel> list = db.PaperLibrary.Where(a => a.status != 2).Select(a => new PaperLibraryListModel
{
i_Name=a.Institution.i_Name,
PaperLibrary_Id=a.PaperLibrary_Id,
PaperLibrary_Title=a.PaperLibrary_Title,
status=a.status
}).ToList();
return View(list);
}
}
3.2添加卷库
页面效果:
核心代码:
/// <summary>
/// 添加卷库--视图
/// </summary>
/// <returns></returns>
public ActionResult AddPaperLibrary()
{
ViewBag.Institution = GetInstitutionForDDL();
return View();
}
/// <summary>
/// 添加卷库--实现功能
/// </summary>
/// <param name="model">卷库信息</param>
/// <returns></returns>
[HttpPost]
public ActionResult AddPaperLibrary(AddPaperLibraryModel model)
{
PaperLibrary pl = new PaperLibrary
{
institution_Id=model.institution_Id,
PaperLibrary_Title=model.PaperLibrary_Title,
status=1
};
using (OnLineExamDB db = new OnLineExamDB())
{
db.PaperLibrary.Add(pl);
db.SaveChanges();
ViewBag.Institution = GetInstitutionForDDL();
return RedirectToAction("PaperLibrary");
}
}
3.3修改卷库
页面效果:
核心代码:
/// <summary>
/// 修改卷库信息--视图
/// </summary>
/// <param name="id">主键编号</param>
/// <returns></returns>
public ActionResult UpdatePaperLibrary(int id)
{
using (OnLineExamDB db = new OnLineExamDB())
{
UpdatePaperLibraryModel model = db.PaperLibrary.Where(a => a.PaperLibrary_Id == id).Select(a => new UpdatePaperLibraryModel
{
institution_Id=a.institution_Id,
PaperLibrary_Id=a.PaperLibrary_Id,
PaperLibrary_Title=a.PaperLibrary_Title
}).FirstOrDefault();
ViewBag.Institution = GetInstitutionForDDL();
return View(model);
}
}
/// <summary>
/// 修改题库信息--功能实现
/// </summary>
/// <param name="model">题库信息</param>
/// <returns></returns>
[HttpPost]
public ActionResult UpdatePaperLibrary(UpdatePaperLibraryModel model)
{
using (OnLineExamDB db = new OnLineExamDB())
{
PaperLibrary pl = db.PaperLibrary.First(a => a.PaperLibrary_Id == model.PaperLibrary_Id);
pl.institution_Id = model.institution_Id;
pl.PaperLibrary_Title = model.PaperLibrary_Title;
db.SaveChanges();
ViewBag.Institution = GetInstitutionForDDL();
return View("QuestionBankList");
}
}
3.4删除卷库
页面效果:
核心代码:
/// <summary>
/// 删除卷库信息(逻辑删除)
/// </summary>
/// <param name="id">主键编号</param>
/// <returns></returns>
[HttpPost]
public ActionResult DelPaperLibrary(int id)
{
using (OnLineExamDB db = new OnLineExamDB())
{
PaperLibrary model = db.PaperLibrary.First(a => a.PaperLibrary_Id == id);
model.status = 2;
db.SaveChanges();
return Json("ok");
}
}
遇到的问题:
针对页面中的细节样式点调整的比较慢。
解决办法:
由于时间原因,先将整体功能实现,针对页面微小的细节的调整,抽时间统一整理改进。
燃尽图: