I'm creating an online exam project.In "add questions from question bank" part I have a problem.I want to send the datas in selected rows to controller but my table codes is a little complex.I want to get the title of questions and grades and even if those are choice questions I want to get choices and correct answers and etc so I created a ViewModel. This is the viewModel:
public class AddQuestionViewModel
{
public int QuestionId { get; set; }
public int ExamId { get; set; }
public string UserId { get; set; }
public decimal Grade { get; set; }
public string questionTitle { get; set; }
public List<ChoiceQuestionSelection> choice { get; set; }
public List<TrueFalseQuestion> trueFalse { get; set; }
public bool IsShow { get; set; }
public bool IsSelect { get; set; }
}
This is the html:
@model IEnumerable<AddQuestionViewModel>
<form id="QuestionsForm" method="post" asp-action="CreateQuestionFromQuestionBank">
<table class="table" id="tblQuestions">
<thead>
<tr>
<th></th>
<th></th>
<th>سوال</th>
<th>نمره</th>
<th>نوع</th>
<th>دستورات</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td><input type="checkbox" /></td>
<td><input type="hidden" name="qId" asp-for="@item.QuestionId"
/>@item.QuestionId</td>
<td>
<input name="questionTitle" asp-for="@item.questionTitle" disabled
value="@item.questionTitle" />
</td>
<td>
<input name="Grade" id="Grade[@i]" asp-for="@item.Grade"
readonly="readonly" value="@item.Grade" />
</td>
@if (item.choice.Any(ch => ch.QuestionId == item.QuestionId))
{
<td>
گزینه ای
<div>
<br />
@foreach (var choice in item.choice.Where(q => q.QuestionId
== item.QuestionId))
{
<div class="form-group" name="choices">
<label class="radio-inline" style="">
<input type="radio" disabled value=""
@((choice.IsTrue) ? "checked" : "")>
<input type="text" value="@choice.Choice" asp-
for="@item.choice" readonly="readonly" />
</label>
</div>
choices++;
}
</div>
</td>
}
else if (item.trueFalse.Any(q => q.QuestionId == item.QuestionId))
{
<td>
صحیح و غلط
<div>
<br />
@foreach (var trueFalse in item.trueFalse.Where(q =>
q.QuestionId == item.QuestionId))
{
<div>
@if (trueFalse.IsTrue)
{
<div>صحیح</div>
}
else
{
<div>غلط</div>
}
</div>
}
</div>
</td>
}
else
{
<td>تشریحی</td>
}
<td></td>
</tr>
i++;
}
</tbody>
</table>
<input name="submit" type="submit" value="save" />
</form>
This is the ajax code:
<script type="text/javascript">
$(':submit').click(function (event) {
event.preventDefault();
$('input:checked').each(function () {
$this = $(this);
stringresult += $this.parent().siblings('td').text();
});
var frm = $('#QuestionsForm');
$.ajax({
url: frm.attr('action'),
method: "POST",
data: stringresult.serialize(),
}).done(function (response) {
window.location.href = response.redirectToUrl;
});
});
</script>
This is choice question model:
public class ChoiceQuestionSelection
{
[Key]
public int ChoiceQuestionSelectionId { get; set; }
public int QuestionId { get; set; }
public string Choice { get; set; }
public bool IsTrue { get; set; }
#region relations
public Question question { get; set; }
#endregion
}
This is truefalse question model:
public class TrueFalseQuestion
{
public int TrueFalseQuestionId { get; set; }
public int QuestionId { get; set; }
public bool IsTrue { get; set; }
#region
public Question question { get; set; }
#endregion
}
This is the controller:
[HttpPost]
public IActionResult CreateQuestionFromQuestionBank(AddQuestionViewModel addQuestions)
{
//to do something
}
I searched a lot but I couldn't find anything like my situation. Thank you in advance.
