【问题标题】:Loop over a FormCollection to get radiobutton SelectedAnswers遍历 FormCollection 以获取单选按钮 Selected Answers
【发布时间】:2014-08-10 17:56:44
【问题描述】:

如何使用表单集合获取以谓词方式命名为 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
}

我的最终目标是将选定的值保存到数据库中。

【问题讨论】:

    标签: c# asp.net-mvc model-view-controller radio-button formcollection


    【解决方案1】:

    搞定了

      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 });
       }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 2012-03-25
      • 1970-01-01
      • 2011-08-05
      • 2012-06-10
      • 2011-11-15
      • 2015-04-20
      • 1970-01-01
      相关资源
      最近更新 更多