-1

VIEW

the increment does not work

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>

Whenever i debug i can't get the value of the radiobutton to my controller

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; } }

i can't get the value on post:( help is so much appreicated :))))

  • What is the model in the view (`@model ????`) and do you mean `foreach (var item in x.MTchoices)`? Your `RadioButtonFor()` method is generating `name` attributes which have no relationship to your model. –  Jan 27 '16 at 12:29
  • @StephenMuecke it's MCchoices i just mistype it. sorry – BeginnerProgrammer Jan 27 '16 at 12:32
  • I assume you not going to post the relevant code or indicate what the mdoel is in the view. Your model is not correct and neither is your usage of a `foreach` loop. Suggest you look at [this answer](http://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) –  Jan 27 '16 at 12:52

1 Answers1

1

Don't use foreach it brokes model binding like Stephen Muekle said.

You should either define EditorTemplate for each model. Or use for loop:

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>

I can be wrong with indexes couse i don't understand why you using 2 nested foreach for MCchoices collection. But i hope you get the idea.

teo van kot
  • 12,350
  • 10
  • 38
  • 70