如何使用表单集合获取以谓词方式命名为 Questions[i] 的单选按钮的值?
在表单集合中
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)
在控制器中
[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
}
我的最终目标是将选定的值保存到数据库中。