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 lamb
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
Welcome to the Quiz
@Html.BeginForm( method:FormMethod.Post, controllerName:"Home", actionName:"index")
{
@for (var i = 0; i < Model.Count; i++)
{
@Html.HiddenFor(x => x[i].QuestionID)
}
}
NOTE: When the form is submitted the POST action could take an IList
model where you will have the answers for each question (in the SelectedAnswer
property).