html - ForEach 循环中的 MVC Razor RadioButtonFor

标签 html asp.net-mvc-4 razor

我是 MVC 的新手。我正在尝试使用单选按钮呈现带有选项的问题。 这是我试过的。我可以使用单选按钮显示带有选项的问题,但我只能在问题中选择一个单选按钮。

型号:

public class CheckListModel()
{
    IEnumerable<QuestionModel> Questions { get; set; }    
}

Public class QuestionModel()
{
    Int Id { get; set; }
    string Question { get; set; }
    IEnumerable<AnswerModel> Answers { get; set; }
}

Public class AnswerModel()
{
    Int Id { get; set; }
    string Answer { get; set; } 
}

查看:

@if (Model != null)
{
  if (Model.CheckListModel != null)
  {
    int i = 0;
    <div class="form-group">
      <fieldset>
      @foreach (var question in Model.CheckListModel.Questions)
      {
        <legend>@question.Question</legend>
        foreach (var answer in question.Answers)
        {
          i++;
          string nameAtt = "Grp" + i.ToString();
          <div class="radio">
            @Html.RadioButtonFor(a => a.eCheckListModel.Questions.Where( q => q.Id == 
              question.Id).FirstOrDefault().Answers.Where(b => b.Id == answer.Id).FirstOrDefault
              ().Answer, answer.Id.ToString(), new { @name = nameAtt, @id = nameAtt })
            @answer.Answer
          </div>
        } 
      }
      </fieldset>
    </div>
  }
}

最佳答案

您的方法行不通,因为每个单选按钮的名称属性没有被正确重命名。使用 ... new { @name = nameAtt,..毫无意义,因为 Html Helpers 会覆盖任何设置 name 属性的尝试。

首先,在 Controller 中填充您的 View 模型(使用与该问题相关的答案填充每个问题)并向 QuestionModel 添加一个属性允许您绑定(bind)选定的答案。

模型

public class CheckListModel
{
  public List<QuestionModel> Questions { get; set; } // use List (easier for indexing the name attribute
  ... // other properties of model
}

public class QuestionModel
{
  public int Id { get; set; }
  public string Question { get; set; }
  public int SelectedAnswer { get; set; } // Add this
  public List<AnswerModel> Answers { get; set; }
}

public class AnswerModel
{
    public int ID { get; set; }
    public string Answer { get; set; }
}

查看

@model YourAssembly.CheckListModel

@using (Html.BeginForm())
{
  ....
  for (int i = 0; i < Model.Questions.Count; i++)
  {
    @Html.HiddenFor(m => m.Questions[i].ID) // for postback
    <p>@Model.Questions[i].Question</p>
    for (int j = 0; j < Model.Questions[i].Answers.Count; j++)
    {
      <div>
        @Html.RadioButtonFor(m => m.Questions[i].SelectedAnswer, Model.Questions[i].Answers[j].ID)
        <span>@Model.Questions[i].Answers[j].Answer</span>
      </div>   
    }
  }
  ....
}

这将呈现输入 <input type="hidden" name="Questions[0].ID".. , <input type="radio" name="Questions[0].SelectedAnswer"..等等

关于html - ForEach 循环中的 MVC Razor RadioButtonFor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25412395/

相关文章:

jquery - 使用javascript重置输入类型文本?

html - 防止取消订阅转发的电子邮件

asp.net-mvc-4 - 替换 mvc4 中的图标

asp.net-mvc - 如何在 Bootstrap 中创建一个保留状态的侧边栏菜单?

javascript - 从 cshtml View 调用 javascript 方法

html - 边界半径内联工作但不在单独的 css 文件中

jquery - 使用jquery在文本框中动态添加值

c# - 使用 Controller 或 View 外部的元数据验证对象

javascript - razor 上的 jquery 选择器不触发事件

javascript - 页面加载后如何调用JQuery函数?