Q:

MVC Razor RadioButtonFor in ForEach loop

I am new to MVC. I am trying to render questions with options using radio buttons.
Here is what I have tried. I am able to display questions with options using radio buttons, but I can select only one radio button across the questions.             
Model:
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; } 
}

View:
@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>
  }
}

A:

Your approach wont work because the name attribute of each radio button is not being renered correctly. Using ... new { @name = nameAtt,.. is pointless because Html Helpers override any attempt to set the name attribute.
Firstly, populate your view models in the controller (populate each question with the answers relevant to that question) and add a property to the QuestionModel that allows you to bind the selected answer.
Models
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; }
}

View
@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>   
    }
  }
  ....
}

This will render inputs <input type="hidden" name="Questions[0].ID".., <input type="radio" name="Questions[0].SelectedAnswer".. etc

