C# – Asp.net MVC Razor how to show grouped radio buttons for two model fields

asp.net-mvcasp.net-mvc-3asp.net-mvc-4crazor

I have a simple quiz model, and I am trying to let the user select Correct Answer/Alternative answer from two radio buttons, grouped , in a strongly typed view. But the lambda expressions I use aren't working. I get two blank radio buttons. I have looked at several questions here, and online but my model is an IList<>, and I can't find a suitable example. All examples I found work with a non-IList<>.

This is my Model

Model:

public partial class Question
    {
        public int QuestionID { get; set; }
        public string QuestionBody { get; set; }
        public string CorrectAnswer { get; set; }
        public string AlternativeAnswer { get; set; }           
    }

My Controller

public ActionResult Index()
        {
            QuizSimpleEntities quizEntities = new QuizSimpleEntities();
            var questions = from p in quizEntities.Questions
                            select p;

            return View(questions.ToList());

        }

My Model:

  @model IList<Quiz.Models.Question>                                

 <h2>Welcome to the Quiz</h2>
  @Html.BeginForm(method:FormMethod.Post,controllerName:"Home",actionName:"index")
    {
        @foreach (var questions in Model)
        {

        <p>@questions.QuestionBody</p>  

        @* How to display the CorrectAnswer and AlternativeAnswer
           as two radio buttons grouped here? I will be posting the selected value back
        }

}

Thank you

Best Answer

You need to have a property on your view model that will hold the selected answer when the form is posted:

public partial class Question
{
    public int QuestionID { get; set; }
    public string QuestionBody { get; set; }
    public string CorrectAnswer { get; set; }
    public string AlternativeAnswer { get; set; }           

    public string SelectedAnswer { get; set; }
}

and then simply loop through the elements of your model and generated the desired markup:

@model IList<Quiz.Models.Question>                                

<h2>Welcome to the Quiz</h2>
@Html.BeginForm( method:FormMethod.Post, controllerName:"Home", actionName:"index")
{
    @for (var i = 0; i < Model.Count; i++)
    {
        @Html.HiddenFor(x => x[i].QuestionID)
        <fieldset>
            <legend>
                @Html.DisplayFor(x => x[i].QuestionBody)
            </legend>
            <ul>
                <li>
                    @Html.HiddenFor(x => x[i].CorrectAnswer)
                    @Html.RadioButtonFor(x => x[i].SelectedAnswer, Model[i].CorrectAnswer)
                    @Html.DisplayFor(x => x[i].CorrectAnswer)
                </li>
                <li>
                    @Html.HiddenFor(x => x[i].AlternativeAnswer)
                    @Html.RadioButtonFor(x => x[i].SelectedAnswer, Model[i].AlternativeAnswer)
                    @Html.DisplayFor(x => x[i].AlternativeAnswer)
                </li>
            </ul>
        </fieldset>
    }

    <button type="submit">OK</button>
}

NOTE: When the form is submitted the POST action could take an IList<Question> model where you will have the answers for each question (in the SelectedAnswer property).