我正在尝试通过从测试库中随机抽取特定数量的问题来构建MC测试。使用单选按钮列出选项。提交测试时,我希望在表格中插入问题ID和所选选项的ID。我设法存储问题ID但不存储所选择的选项。我所选择的所有选项都是NUll值。以下是我的代码。请帮忙。 提前谢谢。
@{
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>
答案 0 :(得分:1)
您需要匹配无线电元素名称,并在帖子后将值从Request
对象中拉回来。
变化:
<input type="radio" name ="@row.ID" value="@row1.ID">
要:
<input type="radio" name="answer-@row.ID" value="@row1.ID" />
并改变:
a=Request.Form["@row.ID"];
b=@item.ID.ToString();
要:
a = Request.Form["answer-" + item.ID];
b = item.ID;