Hi all I Tried to select question based on section id and take the number of question from same table in database. for example I want to show the name of question with 4 possible answered base on section id, also I need to pick number of question base on the amout of question column that show how many question I need to present.
my code below in controller:
public ActionResult Index()
{
var model = (from A in db.Exam_Assessment
join q in db.Exam_Questions on A.Assessment_Section equals q.Assessment_Section
select new Questions
{
Assessment_Section = A.Assessment_Section,
Amount_of_Question = A.Amount_of_Test_Question,
QuestionId = q.QuestionId,
Categories_QuestionName = db.Exam_Questions.Where(x => x.Assessment_Section == A.Assessment_Section).Select(x => x.QuestionName).Take(A.Amount_of_Test_Question).ToList(), ==>>> here when I got some error when I tried to pass number of question(int) to Take()
function inside linq
PossibleAnswers = new List<PossibleAnswer>
{
new PossibleAnswer { Answer = db.Exam_Answers.Where(x=> x.QuestionId == q.QuestionId).Select(x => x.Answer_Option_1).FirstOrDefault() },
new PossibleAnswer { Answer = db.Exam_Answers.Where(x=> x.QuestionId == q.QuestionId).Select(x => x.Answer_Option_2).FirstOrDefault() },
new PossibleAnswer{ Answer = db.Exam_Answers.Where(x=> x.QuestionId == q.QuestionId).Select(x => x.Answer_Option_3).FirstOrDefault() },
new PossibleAnswer { Answer = db.Exam_Answers.Where(x=> x.QuestionId == q.QuestionId).Select(x => x.Answer_Option_4).FirstOrDefault() },
}
}).ToList();
return View(model.ToArray());
}
my view
@*@model IEnumerable<BoatSafetyExam.ViewModels.Student_Exam_ViewModel>*@
@model BoatSafetyExam.ViewModels.Student_Exam_ViewModel[]
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_MasterLayout2.cshtml";
}
<link href="~/Content/Student_Exam.css?v1.6" rel="stylesheet" />
<div class="form-group">
<div class="indent7" style="color:black; font-weight:bold; font-family:Arial" ;padding-top:1px>Read the Questions and answer carefully</div>
<br />
<div class="indent8" style="color:black; font-weight:bold; font-family:Arial" ;padding-top:1px>Submit your answer by clicking on the finished button at the bottom of this page.</div>
</div>
<br />
<hr />
@Html.Partial("_InputValidation")
<h4>
@*You will be logged out in : <span id='timer'></span>*@
</h4>
<div id="hms" hidden="hidden">01:30:00</div>
<script type="text/javascript">
var timeoutHandle;
function count() {
var startTime = document.getElementById('hms').innerHTML;
var pieces = startTime.split(":");
var time = new Date(); time.setHours(pieces[0]);
time.setMinutes(pieces[1]);
time.setSeconds(pieces[2]);
var timedif = new Date(time.valueOf() - 1000);
var newtime = timedif.toTimeString().split(" ")[0];
$("#countTime").html(newtime);
document.getElementById('hms').innerHTML = newtime;
timeoutHandle = setTimeout(count, 1000);
}
count();
</script>
<br />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
List<string> totalQuestion = ViewBag.PickQuestions;
<div class="form-group">
@for (var modelIndex = 0; modelIndex < Model.Length; ++modelIndex)
{
for (var questionIndex = 0; questionIndex < Model[modelIndex].GeneralQuestions.Count; questionIndex++)
{
var question = Model[modelIndex].GeneralQuestions[questionIndex];
var IndexCount = modelIndex + 1;
<div class="form-group">
<div class="col-md-12">
<div class="col-md-7 shadow-sm p-4 mb-4 bg-light font-weight-bold" style="font-family:Arial">
@Html.HiddenFor(m => m[modelIndex].GeneralQuestions[questionIndex].QuestionId)
<ul class="w3-container" style="list-style-type:none;">
<li>
@IndexCount-
@*<label> @totalQuestion[modelIndex]</label>*@
@Html.DisplayFor(m => m[modelIndex].GeneralQuestions[questionIndex].QuestionName,
new
{
@class = "noWrap form-control",
@style = "font-weight:bold;"
}
)
</li>
</ul>
</div>
@for (var answerIndex = 0; answerIndex < Model[modelIndex].GeneralQuestions[questionIndex].PossibleAnswers.Count; ++answerIndex)
{
<div class="col-md-7" style="font-family:Arial">
@Html.RadioButtonFor(m => m[modelIndex].GeneralQuestions[questionIndex].PossibleAnswers,
Model[modelIndex].GeneralQuestions[questionIndex].PossibleAnswers[answerIndex].AnswerId,
new
{
required = "required",
type = "radio",
id = $"_{modelIndex}_.GeneralQuestions_{questionIndex}_.PossibleAnswers.AnswerId_{answerIndex}"
}
)
<label class="noWrapform-control" style="font-family:Arial" for="@($"_{modelIndex}_.GeneralQuestions_{questionIndex}_.PossibleAnswers.AnswerId_{answerIndex}")">
@Model[modelIndex].GeneralQuestions[questionIndex].PossibleAnswers[answerIndex].Answer
</label>
<br />
</div>
<br />
}
</div>
</div>
}
<hr />
}
</div>
<div class="form-group col-md-12 ">
<button id="btnSave"
type="button" onclick="validateAndSubmit();"
class=" btn btn-primary">
FINISHED
</button>
</div>
}
and my view model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Web.Mvc;
using BoatSafetyExam.Global;
using BoatSafetyExam.Models;
namespace BoatSafetyExam.ViewModels
{
public class Student_Exam_ViewModel
{
public List<Questions> GeneralQuestions { get; set; }
public List<string> Categories_QuestionName { get; set; }
public int? AssessmentId { get; set; }
public string Assessment_Section { get; set; }
public List<PossibleAnswer> PossibleAnswers { get; set; }
public int SelectedAnswerdId { get; set; }
]
public class Questions
{
public int QuestionId { get; set; }
public string QuestionName { get; set; }
public List<string > Categories_QuestionName { get; set; }
public int? AssessmentId { get; set; }
public string Assessment_Section { get; set; }
public int Amount_of_Question { get; set; }
public List<PossibleAnswer> PossibleAnswers { get; set; }
public int SelectedAnswerdId { get; set; }
}
public class PossibleAnswer
{
public int? AnswerId { get; set; }
public string Answer { get; set; }
public string Answer_Option1 { get; set; }
public string Answer_Option2 { get; set; }
public string Answer_Option3 { get; set; }
public string Answer_Option4 { get; set; }
}
}