私はオンライン試験に取り組んでいます http get で学生の回答を保存したい http get でチェックボックスをオンにしたいだけです。クエリにそれらを持っています。モデルに追加したいだけです。
私のモデル
public class Question
{
public int QuestionId { get; set; }
public string QuestionName { get; set; }
public int QuestionTypeId { get; set; }
public List<QuestionOption> Options { get; set; }
public int SelectedOption { get; set; }
public List<QuestionOption> SelectedOptioncheckBox { get; set; }
public int TestId { get; set; }
}
public class QuestionOption
{
public int OptionId { get; set; }
public string OptionName { get; set; }
public bool IsChecked { get; set; }
}
私のコントローラー
Question Q = new Question();
Q.SelectedOptioncheckBox = new List<QuestionOption>();
List<int> ChkOptions = studBal.GetCheckedAnswers((int)TestId, model[count].QuestionId, (int)(studBal.getStudentId(Session["sname"].ToString())));
for (int i = 0; i < ChkOptions.Count(); i++)
{
Q.SelectedOptioncheckBox.Add(ChkOptions.ElementAt(i))
}
マイクエリ
var data = (from temp in context.Student_Answer_Master
where temp.Test_Id == TestId && temp.Question_Id == QuestionId && temp.StudentId == StudentId
orderby temp.Option_Id ascending
select (int)temp.Option_Id).ToList();
return data;
私の見解
<div>
@if(chk==null || chk.Count()==0)
{
@Html.CheckBoxFor(m => Model[i].Options[j].IsChecked, new { id = "selectedOption" + (j + 1) })
@Html.HiddenFor(m => Model[i].Options[j].OptionId)
}
else if (chk [0]== Model[i].Options[j].OptionId)
{
@Html.CheckBoxFor(m => Model[i].SelectedOptioncheckBox[j].IsChecked, new { id = "selectedOption" + (j + 1) })
@Html.HiddenFor(m => Model[i].Options[j].OptionId)
}
<span>@Model[i].Options[j].OptionName</span>
</div>
クエリを見るとわかるように、データベースのリストChkOptionsでチェック済みのオプションが表示されます。そのChkOptionsをSelectedOptioncheckBoxであるモデル フィールド に追加して、ビューで既にチェックされているチェック ボックスを表示できるようにします。