【发布时间】:2021-03-09 08:15:26
【问题描述】:
我正在创建一个在线考试项目。在“从题库添加问题”部分我遇到了一个问题。我想将选定行中的数据发送到控制器,但我的表格代码有点复杂。我想得到问题和成绩的标题,即使这些是选择题,我也想获得选择和正确答案等,所以我创建了一个 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; }
}
这是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>
这是ajax代码:
<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>
这是选择题模型:
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
}
这是真假问题模型:
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
}
这是控制器:
[HttpPost]
public IActionResult CreateQuestionFromQuestionBank(AddQuestionViewModel addQuestions)
{
//to do something
}
我搜索了很多,但找不到像我这样的情况。 提前谢谢你。
【问题讨论】:
-
这个问题对你有帮助吗?这里有共同的连贯点。 - stackoverflow.com/questions/52620273/…
标签: jquery ajax asp.net-core