How can I get the values of radio buttons that are named predicatively as Questions[i] using a form collection?
In the form collection
Questions[0].ObjectId
Questions[0].SelectedAnswer
Questions[1].ObjectId
Questions[1].SelectedAnswer
Questions[2].ObjectId
Questions[2].SelectedAnswer
3 - 13 ... etc...
Questions[14].ObjectId
Questions[14].SelectedAnswer
(a bunch of other stuff)
In the Controller
[HttpPost]
public ActionResult PostResults(FormCollection form)
{
List<SelectedAnswer> selectedAnswers = new List<SelectedAnswer>();
for (int i = 0; i < 15; i++)
{
//this is the part that is not working...
selectedAnswers.Add(new SelectedAnswer() { questionId = form["Questions"][i].ObjectId; answerId = form["Questions"][i].SelectedAnswer}
}
//continue to do other stuff
}
My ultimate goal is to save the selected values to the database.
Got this working
for (int i = 0; i < 15; i++)
{
long x = Int64.Parse(form[String.Format("Questions[{0}].ObjectId", i)]);
long y = Int64.Parse(form[String.Format("Questions[{0}].SelectedAnswer", i)]);
if (x > 0 && y > 0)
{
selectedAnswers.Add(new SelectedAnswer() { questionId = x, answerId = y });
}
}