简体   繁体   English

多个答案组和答案asp.net MVC

[英]Multiple answer group & answers asp.net MVC

I want to do a single OR multiple answer quiz using radiobuttonfor and checkboxFor, but I cannot make it work. 我想使用radiobuttonfor和checkboxFor进行单个或多个答案测验,但是我无法使其正常工作。 The problem with all example I see is that the Question model also register the SelectedAnswer, but in my case I want each possible answer to be selectable since some questions will have multiples answers, and thus the isSelected property is directly inside the Answer model. 我看到的所有示例的问题是,问题模型也注册了SelectedAnswer,但是在我的情况下,我希望每个可能的答案都是可选的,因为某些问题将具有多个答案,因此isSelected属性直接在Answer模型内部。

Therefore, for questions with single answers, when I try to create my model using RadioButtonFor(m => m[question].Answers[answerToDisplayId].IsSelected), every answer is in its own group and is not unchecked when I check another answer from that question (basically it behave like a checkBoxFor) 因此,对于具有单个答案的问题,当我尝试使用RadioButtonFor(m => m [question] .Answers [answerToDisplayId] .IsSelected)创建模型时,每个答案都在其自己的组中,当我检查另一个答案时不会被取消选中从这个问题(基本上表现得像一个checkBoxFor)

What I currently have: The question model 我目前所拥有的:问题模型

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; }

Answer model : 答案模型:

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

The view : 风景 :

<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>

The controller : 控制器:

    [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);
    }

What would be the good way to do this? 这样做的好方法是什么? Thank you! 谢谢!

In case someone will see this : 万一有人看到这个:

The solution I found was to change the model : Instead of having one IsSelected parameter per answer, add a List selectedAnswers to your Question model. 我发现的解决方案是更改模型:而不是每个答案具有一个IsSelected参数,而是将List selectedAnswers添加到您的Question模型。 Then, in the view, add your radiobutton like this : 然后,在视图中,像这样添加您的单选按钮:

@Html.RadioButtonFor(m => m[i].SelectedAnswers, Model[i].Answers[j].Id)

You will store the id of each selected answer for this question ine the SelectedAnswers list. 您将在“ SelectedAnswers”列表中存储该问题的每个选定答案的ID。 You can then create your results using this data. 然后,您可以使用此数据创建结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM