循环内的MVC RadioButton不起作用

时间:2018-10-12 10:11:07

标签: c# asp.net-mvc razor radio-button

这是我的Quizz,格式为XMl:

<?xml version="1.0" encoding="utf-8" ?>
    <questions>
      <question level="1" number="1" description="What's the bla blah blah blah and name it?">
        <options>
          <option value="1" iscorrect="0" description="Photograph"></option>
          <option value="2" iscorrect="0" description="Sound"></option>
          <option value="3" iscorrect="1" description="Camera"></option>
        </options>
      </question>
      <question level="1" number="2" description="The sky is Blue , bla bla bla?">
        <options>
          <option value="1" iscorrect="0" description="nothing."></option>
          <option value="2" iscorrect="1" description="victory."></option>
          <option value="3" iscorrect="0" description="defeat"></option>
        </options>
      </question>
<question level="1" number="3" description="This isAnother question in?">
    <options>
      <option value="1" iscorrect="1" description="Donkey."></option>
      <option value="2" iscorrect="0" description="Fox."></option>
      <option value="3" iscorrect="0" description="Lince."></option>
    </options>
  </question>
</questions>

按照以下步骤读取节点并将属性存储在模型中

public class Options 
    {
        public int optionValue { get; set; }
        public Boolean isCorrect { get; set; }
        public string optionDescription { get; set; }
    }

    public class YouthAreaQuizVM
    {
        public List<Questions> questionsList { get; set; }

        public YouthAreaQuizVM()
        {
            questionsList = new List<Questions>();
        }
    }

    public class Questions
    {
        public int questionLevel { get; set; }
        public int questionNumber { get; set; }
        public string questionDescription { get; set; }

        public List<Options> optionsList { get; set; }

        public Questions()
        {
            optionsList = new List<Options>();
        }
    }

在我看来这就是我所拥有的

 @model Model.YouthAreaQuizVM
@using (Html.BeginForm("Quiz", "YouthArea", FormMethod.Post))
{

    @foreach (var questions in Model.questionsList)
                            {
                                <div class="mp-quiz">
                                    <div class="row">
                                        <div class="col-xs-2 col-lg-1">
                                            <span>@questions.questionNumber</span>
                                        </div>
                                        <div class="col-xs-10 col-lg-11">
                                            <div class="form-group">
                                                <h3>@questions.questionDescription</h3>
                                            </div>

                                            @for (var i=0;i<questions.optionsList.Count;i++)
                                            {
                                                <div class="form-group">

                                                    @*@Html.RadioButtonFor(x=>option.optionValue,option.isCorrect, new { @checked = "checked", @class = "mp-radio k-radio" })*@
                                                    @Html.RadioButtonFor(x => questions.optionsList[i].optionValue, questions.optionsList[i].isCorrect, new { @checked = "checked", @class = "mp-radio k-radio" })*@
                                                    @*@Html.RadioButtonFor(c => radio.isCorrect, radio.optionValue)*@
                                                    @*@Html.RadioButtonFor(c => radio.isCorrect, "state14", new { @class = "mp-radio k-radio", @Checked = "checked" })*@
                                                    <label class="k-radio-label mp-radio-label" for="state14">@questions.optionsList[i].optionDescription</label>
                                                </div>     
                                            }                                 
                                        </div>
                                    </div>
                                    <hr>
                                </div>
                            }  }

我的控制器:

public ActionResult Quiz()
        {
            var quiz = new YouthAreaQuizVM();
            List<Questions> questions = new List<Questions>();

            List<Options> options;

            var xDoc = XDocument.Load(Server.MapPath("~/Content/QuizQuestions.xml"));
            var RootElements = xDoc.Root.Elements();

            foreach (XElement node in RootElements)
            {
                options = new List<Options>();

                foreach (XElement nodeChild in node.Element("options").Elements())
                {
                    options.Add(new Options
                    {
                        optionValue = int.Parse(nodeChild.Attribute("value").Value),
                        isCorrect = Convert.ToBoolean(nodeChild.Attribute("iscorrect").Value=="1"?true:false),
                        optionDescription = nodeChild.Attribute("description").Value
                    });
                }
                    questions.Add(new Questions
                    {
                        questionLevel = int.Parse(node.Attribute("level").Value),
                        questionNumber = int.Parse(node.Attribute("number").Value),
                        questionDescription = node.Attribute("description").Value,
                        optionsList = options
                    });
            }

            quiz.questionsList.AddRange(questions); 
            return View(quiz);
        }         

我似乎无法选择任何单选按钮,我认为这与我尝试构建它们的方式有关。我不确定应该如何使用@ Html.RadioButtonFor助手,我需要存储radioButton和Boolean IsCorrect的值,有关在这里应该做什么的任何想法?我不确定我是否以一种有效的方式来执行此操作,以检索正确的,正确的和错误的错误

0 个答案:

没有答案
相关问题