绑定MVC5中的复杂类型

时间:2015-02-05 10:47:51

标签: binding asp.net-mvc-5 radio-button complextype

我是一名新手开发人员,尝试使用asp .net mvc 5开发Web应用程序以供个人使用。网站类型的测验制作者,我可以插入带有意义的俄语单词,并使用这些单词准备测验。

当我尝试编写测验页面并发布数据时,我遇到了一些我无法解决的问题。我遍历模型,读取数据并将它们写入页面。现在,我想要做的是,当我发布表单时,我想获得每个问题字符串和选择的答案(可能采用以下格式:imgur.com/QETnafx)。因此,我可以轻松检查答案字符串是否为真。

我检查了以下教程: 模型绑定到列表由 Phil Haack 和ASP.NET线格式进行模型绑定到数组,列表,集合,字典 Scott Hanselman

我希望我能清楚地解释这个情况。如果您需要更多信息,我很乐意提供。

视图模型

public class QuizInitializationModel
{
    public List<Question> Questions { get; set; }
}

public class QuestionString
{
    public int Id { get; set; }
    public string WordString { get; set; }
}

public class Question
{
    public QuestionString QuestionString { get; set; }
    public List<AnswerItem> Answers { get; set; }
}

public class AnswerItem
{
    public int Id { get; set; }
    public string WordString { get; set; }
}

查看

        @using (Html.BeginForm("Begin", "Quiz", FormMethod.Post))
    {
        <table class="table table-striped table-condensed table-bordered">
            @for (int i = 0; i < Model.Questions.Count; i++)
            {
                <tr>
                    <td>
                        @Html.Label(Model.Questions[i].QuestionString.WordString)
                    </td>
                    <td>
                        @for (int item = 0; item < Model.Questions[0].Answers.Count; item++)
                        {
                            @Html.Label(Model.Questions[i].Answers[item].WordString)@:&nbsp;
                            @Html.RadioButton("array" + "[" + @i + "]" + "." + Model.Questions[i].QuestionString.WordString, Model.Questions[i].Answers[item].Id)<br />
                        }
                    </td>
                </tr>
            }
        </table>
        <input type="submit" value="Send" class="btn btn-primary" />
    }

输出

<form action="/Admin/Quiz/Begin" method="post">
<table class="table table-striped table-condensed table-bordered">
    <tr>
        <td>
            <label for="">вулкан</label>
        </td>
        <td>
            <label for="trade">trade</label>&nbsp;
            <input id="array_0________" name="array[0].вулкан" type="radio" value="18" /><br />
            <label for="volcano">volcano</label>&nbsp;
            <input id="array_0________" name="array[0].вулкан" type="radio" value="24" /><br />
            <label for="talk__conversation">talk, conversation</label>&nbsp;
            <input id="array_0________" name="array[0].вулкан" type="radio" value="15" /><br />
            <label for="time">time</label>&nbsp;
            <input id="array_0________" name="array[0].вулкан" type="radio" value="13" /><br />
            <label for="income">income</label>&nbsp;
            <input id="array_0________" name="array[0].вулкан" type="radio" value="21" /><br />
        </td>
    </tr>
    <tr>
        <td>
            <label for="">мама</label>
        </td>
        <td>
            <label for="universe">universe</label>&nbsp;
            <input id="array_1______" name="array[1].мама" type="radio" value="25" /><br />
            <label for="peace">peace</label>&nbsp;
            <input id="array_1______" name="array[1].мама" type="radio" value="2" /><br />
            <label for="value">value</label>&nbsp;
            <input id="array_1______" name="array[1].мама" type="radio" value="20" /><br />
            <label for="mom__mama">mom, mama</label>&nbsp;
            <input id="array_1______" name="array[1].мама" type="radio" value="17" /><br />
            <label for="industry">industry</label>&nbsp;
            <input id="array_1______" name="array[1].мама" type="radio" value="19" /><br />
        </td>
    </tr>
</table>

我如何修复像“array_1______”这样的标签的ID? 当我将此代码“array”+“[”+ @i +“]”+“。”添加到RadioButton控件以便为每个答案分配索引时,它们就出现了。

1 个答案:

答案 0 :(得分:1)

Html帮助器用下划线替换无效字符(句点实际上不是无效的,但会导致jquery出现问题,因此它也替换了下划线)。但是id属性不是问题,尽管您生成的副本是无效的html。

您手动生成单选按钮的name属性,该属性与模型中的任何属性无关,因此在您回发时不会受到约束。您的模型需要包含可以将所选答案绑定到的属性。将Question模型修改为

public class Question
{
  public QuestionString QuestionString { get; set; }
  public List<AnswerItem> Answers { get; set; }
  public int SelectedAnswer { get; set; } // add this
}

并将视图修改为

@using (Html.BeginForm()) // note parameter not necessary if your posting to the same controller/action
{
  @for (int i = 0; i < Model.Questions.Count; i++)
  {
    ...
    @Html.HiddenFor(m => m.Questions[i].QuestionString.Id)
    <h2>@Model.Questions[i].QuestionString.WordString)</h2>
    ...
    @foreach(var answer in Model.Questions[i].Answers)
    {
      var id = string.Format("{0}-{1}", @i, answer.Id);
      @Html.Label(id, answer.WordString)
      @Html.RadioButtonFor(m => m.Questions[i].SelectedAnswer, answer.ID, new { id = id })
    }
    ....
  }
  <input type="submit" value="Send" class="btn btn-primary" />
}

单选按钮现在绑定到SelectedAnswer属性,当您回发时,该值将是所选AnswerItem的ID

另请注意:

  1. 为问题的ID添加了隐藏的输入,所以 问题可以在回帖后确定
  2. 问题文本位于标题标记中(可能是另一个标记),但是a label不合适 - 标签是与a关联的元素 控制(用于将焦点设置到控件上)但你没有 相关控制
  3. foreach循环中创建一个唯一ID,以便您可以为每个ID 单选按钮一个唯一的id(所以html是有效的)并关联 使用按钮
  4. 标记(答案文本)
相关问题