Question

I am trying to construct MC test by pulling randomly specific number of questions from a test bank. Choices are listed using radio buttons. When test is submitted I wish to insert the ID of question and ID of selected choice in a table. I managed to store the Question ID but not the selected choice. All what I have for selected choice is NUll value. Below is my code. Any help please. Thanks in advance.

    @{
    var db = Database.Open("COMPASSTestItems");
    var SelectedQuestions = db.Query("SELECT TOP 2 * FROM Questions WHERE AreaID = 1 ORDER BY NEWID()");
    var a="";
    var b="";
     if(IsPost){

      foreach(var item in SelectedQuestions){

       a=Request.Form["@row.ID"];
       b=@item.ID.ToString();
       var testresults = "INSERT INTO TestResults (QuestionID,DistractorID) Values(@0, @1)";
      db.Execute(testresults,b,a);
        }
        Response.Redirect("~/Default");
    } 
}

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Test</title>

         <style>
        body
            {
                background-color:  #fcf8e1
            }    
              </style>         




        <script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

         <center><h1>Algebra Test - Answer All Questions Below</h1></center>

    </head>
    <body>

    <div>         



      <form method="post">
         <fieldset>

        <ol>

            @foreach(var row in SelectedQuestions)
            {

              var Dist = db.Query("SELECT * FROM Distractors  WHERE QuestionId = @0 ORDER BY NEWID()",row.ID);

                <li>@row.QStem</li>


                foreach(var row1 in Dist)
                  {

                    <p>    <input  type="radio"  name ="@row.ID" value="@row1.ID">@row1.Distractor   </p><br>                                               


                    } 





            }

        </ol>

          <p><input type="submit" name="buttonSubmit" value="EndTest" /></p>

          </fieldset>
           </form>


    </div>   

  </div>   


    </body>
</html>
Was it helpful?

Solution

You need to match the radio element name and pull the value back out of the Request object after the post.

Change:

<input  type="radio"  name ="@row.ID" value="@row1.ID">

To:

<input type="radio" name="answer-@row.ID" value="@row1.ID" />

And change:

a=Request.Form["@row.ID"];
b=@item.ID.ToString();

To:

a = Request.Form["answer-" + item.ID];
b = item.ID;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top