1

ラジオボタンフォーとチェックボックスフォーを使って、単数または複数回答のクイズをしたいのですが、うまくいきません。私が見るすべての例の問題は、Question モデルも SelectedAnswer を登録することですが、私の場合、一部の質問には複数の回答があり、isSelected プロパティが直接 Answer モデル内にあるため、可能な回答をそれぞれ選択可能にしたいと考えています。

したがって、単一の回答を持つ質問の場合、RadioButtonFor(m => m[question].Answers[answerToDisplayId].IsSelected) を使用してモデルを作成しようとすると、すべての回答が独自のグループにあり、別の回答をチェックしてもチェックが外れませんその質問から(基本的にはcheckBoxForのように動作します)

私が現在持っているもの: 質問モデル

public enum questionfield
{
    Chaser, Beater, Seeker, Contact, Process, Other
};
public enum QuestionDifficulty
{
    Basic, Advanced
};
public enum AnswerType
{
    SingleAnswer, MultipleAnswer
}

public class Question
{
    public int Id { get; set; }
    [Required(ErrorMessage = "Question name not valid")]
    public string Name { get; set; }
    [Required]
    public QuestionField Field { get; set; }
    [Required]
    public QuestionDifficulty Difficulty { get; set; }
    [Required]
    public bool IsVideo { get; set; }
    public string VideoURL { get; set; }
    [Required]
    public string QuestionText { get; set; }
    [Required]
    public AnswerType AnswerType { get; set; }
    [Required]
    public List<Answer> Answers { get; set; }
    [Required]
    public String AnswerExplanation { get; set; }

回答モデル:

public class Answer
{
    public int Id { get; set; }
    public String Answertext { get; set; }
    public Boolean IsTrue { get; set; }
    public Boolean IsSelected { get; set; }
}

景色 :

<div> 
    <!-- For each Question, display a new div with the Title, the question code, the question text, the video if there is one, then the possible answers depending on the type of answers-->
    @using(Html.BeginForm("QuizzResult", "Home"))
    {
        for(int i = 0; i < Model.Count; i++)
        {
            <div class="QuizzQuestion">
                <div class="QuizzQuestionTitle">@Model[i].Id : @Model[i].Name</div> @Html.HiddenFor(m => Model[i].Id)
                <div class="QuizzQuestiontext">@Model[i].QuestionText</div>
                @if(@Model[i].IsVideo)
                {
                    <div class="QuizzQuestionVideoContainer">
                        <iframe class="QuizzQuestionVideo" id="ytplayer" type="text/html"
                                src="@Model[i].VideoURL"
                                frameborder="0"></iframe>
                    </div>
                }
                <div class="RadioButtonAnswers">
                @if (@Model[i].AnswerType == QRefTrain3.Models.AnswerType.SingleAnswer)
                {
                    for (int j = 0; j < Model[i].Answers.Count; j++)
                    {
                        @Model[i].Answers[j].Answertext @Html.RadioButtonFor(m => m[i].Answers[j].IsSelected, true)
                        @Html.HiddenFor(m => Model[i].Answers[j].IsTrue)
                    }
                }
                </div>
            </div>
        }
        <input type="submit" value="Validate Answers"/>
    }
</div>

コントローラ:

    [HttpPost]
    public ActionResult QuizzResult(List<Question> answers)
    {
        foreach(Question a in answers)
        {
            var b = Request.Form[a.Id.ToString()];

        }
        Result result = new Result();
        foreach (Question q in answers)
        {
            result.QuestionsAskedIds.Add(q.Id);
            if (Question.IsGoodAnswer(q))
            {
                result.GoodAnswersIds.Add(q.Id);
            }
        }
        if (User.Identity.IsAuthenticated)
        {
            result.User = Dal.Instance.GetUserByName(HttpContext.User.Identity.Name);

            Dal.Instance.CreateResult(result);
        }

        return View("QuizResult", result);
    }

これを行う良い方法は何ですか?ありがとうございました!

4

1 に答える 1