如何获得Radiobuttonfor的值(使用循环创建)

时间:2016-01-27 12:25:24

标签: asp.net asp.net-mvc asp.net-mvc-4 razor

查看

增量不起作用

int i = 0; 
foreach (var x in Model)
{
<div>

      @if(x.MCchoices.Any())
      {
          foreach (var item in x.MCchoices)
          {
             @item.question_desc
            foreach(var choices in item.MCchoices)
            {
                   @Html.RadioButtonFor(model => choices.qc_selectedchoice, choices.qc_id)
                   @Html.DisplayFor(model => choices.qc_choice)
            }
          }
      }
@{i++;}
</div>

每当我调试时,我都无法将radiobutton的值传给我的控制器

MODEL

public class QuizMaker
{
   public string question_desc { get; set; }
    public string question_id { get; set; }
    public string qc_choice { get; set; }
    public string qc_id { get; set; }
    public string qc_selectedchoice { get; set; }

   public IEnumerable<QuizMaker> MCchoices { get; set; } }

我无法获得帖子的价值:(帮助是如此多的appreicated :))))

1 个答案:

答案 0 :(得分:1)

不要像使用Stephen Muekle那样使用它来制作模型绑定。

您应该为每个模型定义EditorTemplate。或者使用for循环:

for (int i = 0; i < Model.Count(); i++)
{
<div>

      @if(Model[i].MCchoices.Any())
      {
          for (int j = 0; j < Model[i].MCchoices.Count(); j++)
          {
            @Model[i].MCchoices[j].question_desc
            for (int t = 0; t < Model[i].MCchoices[j].Count(); t++)
            {
                   @Html.RadioButtonFor(model => Model[i].MCchoices[j].MCchoices[k].qc_selectedchoice, Model[i].MCchoices[j].MCchoices[k].qc_id)
                   @Html.DisplayFor(model => Model[i].MCchoices[j].MCchoices[k].qc_choice)
            }
          }
      }
</div>

我可能错了索引,我不明白为什么你使用2个嵌套foreach进行MCchoices收集。但我希望你明白这一点。