Asp.net 如何将数据模型绑定到多组单选按钮?

Asp.net 如何将数据模型绑定到多组单选按钮?,asp.net,asp.net-mvc,razor,Asp.net,Asp.net Mvc,Razor,我需要向用户提供是/否类型问题的列表。以下是我的模型: class QuestionModel { public String ID {get; set;} public String Title {get; set; } public bool Value {get; set; } } class QuestionsModel { List<QuestionModel> Questions {get; set} } 但是,我看不出Ques

我需要向用户提供是/否类型问题的列表。以下是我的模型:

 class QuestionModel {
    public String ID {get; set;}
    public String Title {get; set; }
    public bool Value {get; set; }
 }

 class QuestionsModel {
    List<QuestionModel> Questions {get; set}
 }

但是,我看不出QuestionModel中的值字段将如何根据用户选择的内容填充。请帮忙。注意。

首先,您需要使用
for
循环或
EditorTemplate
生成集合,否则控件将无法正确命名,无法在回发时绑定

@model QuestionsModel
@(using Html.BeginForm())
{
  for (int i = 0; i < Model.Questions.Count; i++)
  {
    string yes = i + "Yes"; 
    string no = i + "No";
    @Html.HiddenFor(m => m.Questions[i].ID)
    @Html.DisplayFor(m => m.Questions[i].Title) // assuming you don't want to edit this
    @Html.RadioButtonFor(m => m.Questions[i].Value, true, new { id = @yes })
    <label for="@yes">Yes</label>
    @Html.RadioButtonFor(m => m.Questions[i].Value, false, new { id = @no })
    <label for="@no">No</label>
  }
  <input type="submit" value="Save" />
}
@模型问题模型
@(使用Html.BeginForm())
{
对于(int i=0;im.Questions[i].ID)
@Html.DisplayFor(m=>m.Questions[i].Title)//假设您不想编辑这个
@(m=>m.Questions[i].Value,true,新的{id=@yes})
对
@(m=>m.Questions[i].Value,false,新的{id=@no})
不
}
}
@model QuestionsModel
@(using Html.BeginForm())
{
  for (int i = 0; i < Model.Questions.Count; i++)
  {
    string yes = i + "Yes"; 
    string no = i + "No";
    @Html.HiddenFor(m => m.Questions[i].ID)
    @Html.DisplayFor(m => m.Questions[i].Title) // assuming you don't want to edit this
    @Html.RadioButtonFor(m => m.Questions[i].Value, true, new { id = @yes })
    <label for="@yes">Yes</label>
    @Html.RadioButtonFor(m => m.Questions[i].Value, false, new { id = @no })
    <label for="@no">No</label>
  }
  <input type="submit" value="Save" />
}