c# mvc5+ef 实现简单答题系统5 计算最后一页
这里基本全完成了。
到时我会将项目分享~~~~
一、首先计算出总共好多页面。可以使用db.count().反正一页一个题,要是和当前的page相同,则表示最后一页了,跳转到end页面,否则下一页。
二。代码
public ActionResult AnswersContent(string Id, int? page)
{
if (Id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
///读出背景
KaosiList kaosiList = db.KaosiLists.Find(Id);
ViewBag.bgimg = kaosiList.BgContent;
ViewBag.title = kaosiList.Title;
///当没有ID值的时候,表示page=1
if (string.IsNullOrEmpty(Id) && !page.HasValue)
{
page = 1;
ViewBag.page = page;
}
///这是PagedList要求的内容 ,传递值
ViewBag.Id = Id;
ViewBag.page = page;
///先读出所有的文章,并按时间排序
var seachindex = db.KaoShis.Where(x => x.KaoShiId == Id).OrderBy(c => c.SortInt);
ViewBag.pagesunint = seachindex.Count();
int pageNumber = page ?? 1;
int pageSize = 1;
//ViewBag.page = pagec
//通过ToPagedList扩展方法进行分页
//IPagedList<CmsContent> pagedList = seachindex.ToPagedList(pageNumber, pageSize);
return View(seachindex.ToPagedList(pageNumber, pageSize));
}
前台代码
@model PagedList.IPagedList<myks.Models.KaoShi>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" />
@{
Layout = null;
}
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>答题</title>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
<style>
.col-center-block {
float: none;
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top row">
<div class="container" style="line-height:40px;margin-left:10px;">
@ViewBag.title 【得分 <span id="chtmlsort">..</span> 】
</div>
</nav>
<script type="text/javascript">
var mmmm = getCookie('usersorce');
document.getElementById("chtmlsort").innerHTML = mmmm;
function getCookie(name) {
var arr = document.cookie.split('; ');
for (var i = 0; i < arr.length; i++)
{
var temp = arr[i].split('=');
if (temp[0] == name) { return temp[1]; }
}
return '';
}
</script>
<div class="col-md-4 col-xs-12 col-sm-12 col-center-block" style="padding-left:0px; padding-right:0px;">
<div style=" background:url(@ViewBag.bgimg)no-repeat; width:100%;height:100%; ">
<div class="container text-center">
@foreach (var item in Model)
{
<div class="contmaring"> 题目: @Html.DisplayFor(modelItem => item.KaoTiTitle) </div>
<div class="contAD" id="divaaa"><a href="#" id="aaa" onclick="myFunction('A')">A: @Html.DisplayFor(modelItem => item.KaoTiA)</a> </div>
<div class="contAD" id="divbbb"><a href="#" id="bbb" onclick="myFunction('B')">B: @Html.DisplayFor(modelItem => item.KaoTiB)</a> </div>
<div class="contAD" id="divccc" ><a href="#" id="ccc" onclick="myFunction('C')">C: @Html.DisplayFor(modelItem => item.KaoTiC)</a> </div>
<div class="contAD" id="divddd"><a href="#" id="ddd" onclick="myFunction('D')">D: @Html.DisplayFor(modelItem => item.KaoTiD)</a> </div>
<div class="contAD hidden" id="duil"> 祝贺你,答对了!!!! </div>
<div class="contAD hidden pull-right" id="nextti">@Html.ActionLink("下一题", "AnswersContent", new { Id = ViewBag.Id, page = ViewBag.page + 1 }) </div>
<div class="contAD hidden pull-right" id="nextend">@Html.ActionLink("下一题", "AnswersEnd", new { Id = ViewBag.Id }) </div>
<div class="contAD hidden" id="cuol">你回答: 错了!!! 正确答是 [ @Html.DisplayFor(modelItem => item.CorrectOptions) ] </div>
<div class="contAD hidden" id="jiesuo">解说: @Html.DisplayFor(modelItem => item.Explain) </div>
<script>
function myFunction(a) {
var x = '@Html.DisplayFor(modelItem => item.CorrectOptions)' ;
if (a == x) {
document.getElementById("duil").className = "contAD show"; //更换新样式名
document.getElementById('aaa').onclick = null;
document.getElementById("bbb").onclick = null;
document.getElementById("ccc").onclick = null;
document.getElementById("ddd").onclick = null;
var xsorce='@Html.DisplayFor(modelItem => item.Score)';
var isorce = parseInt(xsorce);
var newsorce = getCookie('usersorce');
var newisorce = parseInt(newsorce);
var subsocre = isorce + newisorce;
document.getElementById("chtmlsort").innerHTML = subsocre;
document.cookie = "usersorce=" + subsocre + ";path=/";
var sunpageint =@ViewBag.pagesunint;
var pagedangqian = @ViewBag.page;
if (sunpageint == pagedangqian) {
window.location.href = "/KaosiLists/AnswersEnd/" + "@ViewBag.Id";
}
else {
var pagen = parseInt('@ViewBag.page') + 1
window.location.href = "@ViewBag.Id" + "?page=" + pagen;
}
}
else {
document.getElementById("cuol").className = "contAD show"; //更换新样式名
document.getElementById("jiesuo").className = "contAD show"; //更换新样式名
var sunpageint =@ViewBag.pagesunint;
var pagedangqian = @ViewBag.page;
if (sunpageint == pagedangqian) {
document.getElementById("nextend").className = "show contAD";
document.getElementById('nextend').style.backgroundColor = '#f7a97c';
document.getElementById('nextend').style.textAlign = 'center';
}
else
{
document.getElementById("nextti").className = "show contAD";
document.getElementById('nextti').style.backgroundColor = '#f7a97c';
document.getElementById('nextti').style.textAlign = 'center';
}
document.getElementById('aaa').onclick = null;;
document.getElementById("bbb").onclick = null;
document.getElementById("ccc").onclick = null;
document.getElementById("ddd").onclick = null;
if (x == 'A') { document.getElementById('divaaa').style.backgroundColor = '#80f19a'; };
if (x == 'B') { document.getElementById('divbbb').style.backgroundColor = '#80f19a'; };
if (x == 'C') { document.getElementById('divccc').style.backgroundColor = '#80f19a'; };
if (x == 'D') { document.getElementById('divddd').style.backgroundColor = '#80f19a'; }
}
}
</script>
}
</div>
</div>
@*<div>
@Html.ActionLink("下一页", "AnswersContent", new { Id = ViewBag.Id , page = ViewBag.page + 1})
</div>
<div class="row">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("AnswersContent",
new { page, Id = ViewBag.Id }))
</div>*@
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
</body>
</html>
浙公网安备 33010602011771号