使用asp.net MVC中的模型获取测验答案

时间:2017-08-11 11:19:46

标签: asp.net asp.net-mvc

作为学习asp.net的一部分,我试图建立一个简单的测验网站:用户选择一些参数,我们使用这些参数生成测验页面(带循环),用户检查他的答案并点击按钮,软件重定向到检查答案的方法并显示结果。

我目前已经生成了问题页面,但是我找不到让用户输入直接在我创建的viewModel中的方法,甚至是在写入该字母的过程之后。这里是ViewModel:

public class QuestionViewModel
{
    public Question Question { get; set; }
    public List<String> Answers { get; set; }
}

这里是问题对象:

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<String> PossibleAnswers { get; set; }
        [Required]
        public List<String> CorrectAnswers { get; set; }

        public Question(string name, QuestionField field, QuestionDifficulty difficulty, bool isVideo, string videoUrl, string questionText, AnswerType answerType, 
            List<String> possibleAnswers, List<String> correctAnswers)
        {
            this.Name = name;
            this.Field = field;
            this.Difficulty = difficulty;
            this.IsVideo = isVideo;
            if(IsVideo == true) { this.VideoURL = videoUrl; } else { this.VideoURL = null; }
            this.QuestionText = questionText;
            this.AnswerType = answerType;
            this.PossibleAnswers = possibleAnswers;
            this.CorrectAnswers = correctAnswers;
        }

控制器包含我的所有方法:

public ActionResult Homepage()
        {
            return View();
        }

        /// <summary>
        /// Load questions according to parameters, then load a quizz page with those questions
        /// </summary>
        /// <param name="id"></param>
        public ActionResult MovetoQuizz()
        {
            Random rnd = new Random();
            List<Question> allQuestions = Dal.Instance.getAllQuestions();
            List<Question> displayedQuestions = new List<Question>();
            // In case we don't have enough questions
            if(allQuestions.Count < 10)
            {
                displayedQuestions = allQuestions;
            }
            else
            {
                while(displayedQuestions.Count < 10)
                {
                    Question question = allQuestions[rnd.Next(allQuestions.Count - 1)];
                    if(!displayedQuestions.Contains(question))
                    {
                        displayedQuestions.Add(allQuestions[rnd.Next(allQuestions.Count - 1)]);
                    }
                }
            }
            List<QuestionViewModel> questionViewModels = new List<QuestionViewModel>();
            foreach(Question q in displayedQuestions)
            {
                questionViewModels.Add(new QuestionViewModel { Question = q, Answers = new List<String>() });
            }
            return View("Quizz", questionViewModels);
        }

        [HttpPost]
        public ActionResult QuizzResult(List<QRefTrain.ViewModel.QuestionViewModel> viewModels)
        {

            // Do stuff here
            return View();
        }

我的问题出在用户验证测验后调用的QuizzResult(不要介意&#34;返回视图()&#34;它在这里没有错误)

最后,观点:

@model List<QRefTrain.ViewModel.QuestionViewModel>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Quizz</title>
</head>
<body>
    <div> 
        <!-- Foreach 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"))
        {
            foreach(QRefTrain.ViewModel.QuestionViewModel questionViewModel in Model)
            {
                <div>
                    <span>@questionViewModel.Question.Name</span>   <span>@questionViewModel.Question.Id</span>
                    <br/>
                    <span>@questionViewModel.Question.QuestionText</span>
                    <br />
                    @if(@questionViewModel.Question.IsVideo)
                    {
                        <iframe id="ytplayer" type="text/html" width="640" height="360"
                                src="@questionViewModel.Question.VideoURL"
                                frameborder="0"></iframe>
                    }
                    <br />
                    @if(@questionViewModel.Question.AnswerType == QRefTrain.Models.AnswerType.SingleAnswer)
                    {
                        foreach(String answerText in questionViewModel.Question.PossibleAnswers)
                        {
                            @answerText @Html.RadioButtonFor(m => m.Find(x => x.Question.Id == questionViewModel.Question.Id).Answers, answerText)
                        }
                    }
                   <!-- else
                    {
                        foreach(String answerText in questionViewModel.Question.PossibleAnswers)
                        {
                          @* @answerText @Html.CheckBoxFor(m => m.Find(x => x.Question.Id == questionViewModel.Question.Id).Answers, answerText) *@
                        }
                    }-->
                </div>
                <br /><br />
            }
            <input type="submit" value="Validate Answers"/>
        }
    </div>
</body>
</html>

根据我的理解,这应该是强类型的,它允许我直接将我的viewModel更改为用户输入内容,而不必自己获取任何内容。

现在,在我转到QuizzResult方法之后,我甚至无法恢复我的modelViews。我究竟做错了什么 ?这是做这件事的好方法吗?谢谢你的帮助。

0 个答案:

没有答案