Hi kingkomand2o,
Refer below sample.
Model
public class InExam
{
public int Id { get; set; }
public AutoTest TheTest { get; set; }
public List<InTest> TheQuestions { get; set; }
public IEnumerable<Result> SingleQuee { get; set; }
}
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
using (KafoEntities db = new KafoEntities())
{
InExam model = new InExam();
model.TheTest = db.AutoTests.Where(x => x.id == 1).FirstOrDefault();
model.TheQuestions = db.InTests.Where(x => x.ExamId == model.TheTest.id).OrderByDescending(x => x.id)
.Take(Convert.ToInt32(model.TheTest.QuestionsNumber)).ToList();
model.SingleQuee = db.Results.ToList();
return View(model);
}
}
[HttpPost]
public ActionResult Test(int? id)
{
List<QuestionAnswer> questionAnswers = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<QuestionAnswer>>(Request.Form["hfSelected"]);
using (KafoEntities db = new KafoEntities())
{
int result = 0;
foreach (QuestionAnswer qa in questionAnswers)
{
if (qa.SelectedValue == "1")
{
result++;
}
}
TempData["Message"] = string.Format("{0} correct answer out of {1}", result, questionAnswers.Count);
}
return RedirectToAction("Result");
}
public ActionResult Result()
{
return View();
}
public class QuestionAnswer
{
public int QuestionId { get; set; }
public string Question { get; set; }
public string SelectedValue { get; set; }
}
}
View
Index
@model Quiz_Question_MVC.Models.InExam
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function () {
$('#btnEnd').on('click', function () {
var selectedAnswers = [];
$.each($('#dvQuestions input[type=radio]:checked'), function () {
var questionAnswer = {};
questionAnswer.QuestionId = parseInt($(this).closest('.container').parent().find('#hfQuestionId').val().trim());
questionAnswer.Question = $(this).closest('.container').parent().find('#lblQuestion').html().trim();
questionAnswer.SelectedValue = $(this).val().trim();
selectedAnswers.push(questionAnswer);
});
$('#hfSelected').val(JSON.stringify(selectedAnswers));
});
});
</script>
</head>
<body>
@using (Html.BeginForm("Test", "Home", new { id = Model.TheTest.id }, FormMethod.Post))
{
<input type="hidden" id="hfSelected" name="hfSelected" value="" />
foreach (var item in Model.TheQuestions)
{
Result singleQuee = Model.SingleQuee.Where(x => x.Question == item.Question).FirstOrDefault();
<div class="container" style="padding-top:50px;direction:rtl;" id="dvQuestions">
<h4 id="lblQuestion" style="text-align:right;font-weight:bold;">@item.Question</h4>
<input type="hidden" id="hfQuestionId" name="hfQuestionId" value="@item.id" />
<div class="container">
<div class="row" style="direction:rtl;">
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
@Html.RadioButton("Question" + @item.id, "1", new { @class = "form-control", id = "Question" + item.id })
<h5 style="padding-top:3px;padding-right:8px;">@item.RightAnswer</h5>
</div>
</div>
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
@Html.RadioButton("Question" + @item.id, "2", new { @class = "form-control", id = "Question" + item.id })
<h5 style="padding-top:3px;padding-right:8px;">@item.Answer2</h5>
</div>
</div>
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
@Html.RadioButton("Question" + @item.id, "3", new { @class = "form-control", id = "Question" + item.id })
<h5 style="padding-top:3px;padding-right:8px;">@item.Answer3</h5>
</div>
</div>
<div class="col-lg-7" style="text-align:right;margin-right:10px;">
<div class="row">
@Html.RadioButton("Question" + @item.id, "4", new { @class = "form-control", id = "Question" + item.id })
<h5 style="padding-top:3px;padding-right:8px;">@item.Answer4</h5>
</div>
</div>
</div>
</div>
</div>
}
<button id="btnEnd" class="btn botton" type="submit">END</button>
}
</body>
</html>
Result
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Result</title>
</head>
<body>
<div>
@if (TempData["Message"] != null)
{
<h4>@TempData["Message"]</h4>
}
</div>
</body>
</html>