1

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"];
       [email protected]();
       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>

1 Answer 1

1

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="[email protected]" value="@row1.ID" />

And change:

a=Request.Form["@row.ID"];
[email protected]();

To:

a = Request.Form["answer-" + item.ID];
b = item.ID;
Sign up to request clarification or add additional context in comments.

7 Comments

I think you ment replacing "a". When I did that, I have noticed that the inserted values of the questions are not those of the displayed. It seems the query to select questions is executed twice: once when the from is displayed and another when the post is completed. Is that possible< If so, what should I do? When there is a match between the questions ID, the value of the selected radio button is inserted, otherwise, it is NULL.
@user3139268 Sorry, I misread and I have updated my answer. Try and avoid using unsemantic variable names though, it leads to confusion.
Okay. I am still facing an issue that item.IDs passed to the results table are different from those displayed in the from. It seems every time the Query "SelectedQuestions", it is executed and different set of IDs are selected????
You are selecting two random questions. Replace the ORDER BY clause with something more suitable.
OKAY! Problem is solved now. I have included each list of radio buttons in a separate from by mistake and that's why only one of the forms was posted. once removed, all selected radio button were inserted. Thank you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.