笔试在线考试系统-参加考试

------------恢复内容开始------------

今日完成功能

由于该功能模块略微复杂,使用2天时间完成。今日一次性更新

1、参加考试

1.1登录今日考试系统(暂未设置页面效果,只实现了功能部分)

页面效果:

核心代码:

/// <summary>
/// 登录-视图
/// </summary>
/// <returns></returns>
public ActionResult Login()
{
return View();
}
/// <summary>
/// 登录--实现功能
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Login(LoginModel model)
{
using (OnLineExamDB db = new OnLineExamDB())
{
List<StuInfo> list = db.StuInfo.Where(a => a.stu_No == model.stu_No && a.password == model.password).ToList();
if (list.Count > 0)
{
System.Web.HttpContext.Current.Session["StuInfo"] = list.First();
StuInfo stu = db.StuInfo.First(a => a.stu_No == model.stu_No && a.password == model.password);
stu.LoginDate = DateTime.Now;//修改登录时间
stu.LoginTimes = stu.LoginTimes + 1;//修改登录次数
db.SaveChanges();
return RedirectToAction("UserCenter");
}
else
{
ViewBag.Message = "用户名或密码有误!";
return View(model);
}
}
}

1.2进入考试系统,显示试卷

(暂未设置页面效果,只实现了功能部分)

试卷分为3种类型

1:统一考试:所有考生试卷一样

2:随机试题:每个考生参考时间相同,每个科目数量相同,但题不完全相同,每个人的都是随机生成

3:模拟测试:学生可以自己选择科目进行模拟,题的总数量为50道,不限制每一科题的数量,自动生成学生选择的科目中的50道

页面效果:

 

核心代码: 

3种类型显示试卷效果类似,此处只粘贴一种考试方式的核心代码

/// <summary>
/// 根据卷库编号显示该卷库下的试卷
/// </summary>
/// <param name="id">卷库编号</param>
/// <returns></returns>
public ActionResult ExamIndex(int id)
{
using (OnLineExamDB db = new OnLineExamDB())
{
StuInfo stu = System.Web.HttpContext.Current.Session["StuInfo"] as StuInfo;
List<PaperModel> list = db.Paper.Where(a => a.status == 1).Select(a => new PaperModel
{
i_Name = a.PaperLibrary.Institution.i_Name,
rightAnswer = a.QuestionBank.rightAnswer,
createTime = a.createTime,
Duration = a.PaperLibraryDetail.Duration,
PaperLibrary_Title = a.PaperLibrary.PaperLibrary_Title,
paper_Id = a.paper_Id,
PaperLibrary_Id = a.PaperLibrary_Id,
major_Id = a.PaperLibraryDetail.major_Id,
question_Id = a.question_Id,
question_Option1 = a.QuestionBank.question_Option1,
question_Option2 = a.QuestionBank.question_Option2,
question_Option3 = a.QuestionBank.question_Option3,
question_Option4 = a.QuestionBank.question_Option4,
question_Title = a.QuestionBank.question_Title,
exam_Type = a.PaperLibraryDetail.exam_Type

}).ToList().Where(a => a.PaperLibrary_Id == id && a.major_Id == stu.major_Id && a.exam_Type == 1).ToList();
return View(list);
}
}

1.3提交试卷

单击提交试卷后,在试卷页面底部显示试卷得分和错题及对应的正确答案

页面效果:

 核心代码:

<script type="text/javascript">
$(function () {
//提交试卷
$("#BtnSubmit").click(function () {
var Answer = "";
$("#result").html("");
//循环遍历页面中的题是否有未做的
var a = 0;
var nums = "";
var paper_id = "";
$("input[type='text']").each(function () {
if ($(this).val() == "") {
a = 1;
}
})
//如果有未完成的题a=1,则提示试卷不能提交
if (a == 1) {
alert("您的题未完成不能提交试卷!")
}
else {
//将每题答案存储在数组中
window.scrollTop = 200;
$("input[type='text']").each(function () {
Answer += $(this).val() + ";";
})
var t = 1;
//$("input[type='hidden']").each(function () {
$("input[name='question_Id']").each(function () {
if (t >= 1 && t <= 51) {
nums += $(this).val() + ";";
}
t++;
})
var x = 1;
$("input[name='paper_Id']").each(function () {
if (x >= 1 && x <= 51) {
paper_id += $(this).val() + ";";
}
x++;
})
//提交答案
$.ajax(
{
type: "post",
url: "/Exam/SubmitPaper",
data: { "a": Answer, "n": nums ,"p": paper_id},
success: function (result) { //返回的结果自动放在resut里面了
$("#BtnSubmit").attr("disabled", true);
$("#result").append(result);
}
});

}
})
})
</script>

/// <summary>
/// 提交试卷
/// </summary>
/// <returns></returns>
[HttpPost]
public string SubmitPaper()
{
string result = "<div>以下是您出错的题(红色是错误答案,小括号内是正确答案),请认真阅读并选择:<br/>";//结果
string answer = Request["a"];// 接收用户的答案
string[] answers = answer.Split(';');
string num = Request["n"];//题号
string[] nums = num.Split(';');
string[] paper_Ids = Request["p"].Split(';');
int Exam_Id = 1;//试卷编号
using (OnLineExamDB db = new OnLineExamDB())
{
List<ScoreInfo> list = db.ScoreInfo.ToList();
if (list.Count > 0)
{
Exam_Id = Convert.ToInt32(list.Max(a => a.Exam_Id)) + 1;
}
}

int f = 0;//错误数
for (int i = 0; i < nums.Length-1; i++)
{
QuestionBank q = new QuestionBank();
using (OnLineExamDB db = new OnLineExamDB())
{
//q = (from a in db.QuestionBank where a.num == Convert.ToInt32(nums[i]) select a).First();
int question_Id = Convert.ToInt32(nums[i]);
q = db.QuestionBank.First(a => a.question_Id == question_Id);

//将数据添加到成绩表中

int score_Result = 1;//结果(1:正确 2:错误)
string answer_Owner = answers[i];
if (answers[i] != q.rightAnswer)
{
if ((f + 1) % 10 == 0)
{
result += "<br/><br/>";
}
else
{
score_Result = 2;
result += (i + 1) + ":<font color='red'>" + answers[i] + "</font>(" + q.rightAnswer + ")&nbsp;&nbsp;&nbsp;&nbsp;";
}
f++;
}


ScoreInfo score = new ScoreInfo();
score.createTime = DateTime.Now;
score.Exam_Id = Exam_Id;
score.result = score_Result;
score.answer_Owner = answer_Owner;
score.paper_Id =Convert.ToInt32(paper_Ids[i]);
db.ScoreInfo.Add(score);
db.SaveChanges();

}

}
StuInfo stu = System.Web.HttpContext.Current.Session["StuInfo"] as StuInfo;
result += "<font style='font-size:24px;'><br/><br/>" + stu.stu_Name + "您" + DateTime.Now + "笔试模拟考试得分为:<font style='font-size:40px;font-weight:bolder; color:red;'>" + (50 - f) * 2 + "</font>分<br/><br/>答对:" + (50 - f) + "道&nbsp;&nbsp;&nbsp;&nbsp;<font color='red'>答错:" + f + "道</font></font></div>";
//此处使用返回json格式更合适,由于时间原因后期再做调整


return result;
}

暂无遇到问题,下次做本功能模块的页面。

原文地址:https://www.cnblogs.com/chengyp/p/13373663.html