我正在使用ASP.NET MVC创建一个简单的调查。 管理员将仅通过3个选项创建问题 - 非常同意,有点同意,非常不同意)通过创建视图。
然后,用户必须使用radiobutton选择他们的答案。
目前,这就是我的观点的样子(我知道这仍然是错误的,因为我仍然试图了解如何正确地做到这一点):
<table class="table">
<tr>
<th>
Question
</th>
<th class="text-center">
Strongly Agree
</th>
<th class="text-center">
Somewhat Agree
</th>
<th class="text-center">
Strongly Disagree
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Question)
</td>
<td align="center">
@Html.RadioButtonFor(modelItem => item.Answer, "Strongly Agree", new { QuestionID = item.QuestionID })
</td>
<td align="center">
@Html.RadioButtonFor(modelItem => item.Answer, "Somewhat Agree", new { QuestionID = item.QuestionID })
</td>
<td align="center">
@Html.RadioButtonFor(modelItem => item.Answer, "Strongly Disagree", new { QuestionID = item.QuestionID })
</td>
</tr>
}
</table>
我的表格如下:
tblTestProper: [QuestionID
,UserID
,Answer
]
tblQuestions: [QuestionID
,Question
]
我可以遵循任何示例或想法来实现这一目标吗?谢谢你的帮助。
答案 0 :(得分:2)
<table class="table">
<tr>
<th></th>
<th></th>
<th>Question</th>
<th></th>
<th></th>
<th class="text-center">Strongly Agree</th>
<th class="text-center">Somewhat Agree</th>
<th class="text-center">Strongly Disagree</th>
</tr>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>@Html.HiddenFor(m => m[i].AnswerID)</td>
<td>@Html.HiddenFor(m => m[i].QuestionID)</td>
<td>@Html.DisplayFor(m => m[i].Question)</td>
<td>@Html.HiddenFor(m => m[i].QuestionTag)</td>
<td align="right">@Html.ValidationMessageFor(m => m[i].Answer, "", new { @class = "text-danger" })</td>
<td align="center">@Html.RadioButtonFor(m => m[i].Answer, 1)</td>
<td align="center">@Html.RadioButtonFor(m => m[i].Answer, 2)</td>
<td align="center">@Html.RadioButtonFor(m => m[i].Answer, 3)</td>
</tr>
}
</table>
答案 1 :(得分:0)
我希望您有所帮助
像这样的HTML代码 <label class="form-radio form-normal active form-text">1<input type="radio" class="rdomcqans" name="qug1" value="1"></label>
<label class="form-radio form-normal active form-text">2<input type="radio" class="rdomcqans" name="qug1" value="2"></label>
<label class="form-radio form-normal active form-text">3<input type="radio" class="rdomcqans" name="qug1" value="3"></label>
<label class="form-radio form-normal active form-text">4<input type="radio" class="rdomcqans" name="qug1" value="4"></label>
<!--qug1 = Question Group 1-->
</br>
<label class="form-radio form-normal active form-text">1<input type="radio" class="rdomcqans" name="qug2" value="1"></label>
<label class="form-radio form-normal active form-text">2<input type="radio" class="rdomcqans" name="qug2" value="2"></label>
<label class="form-radio form-normal active form-text">3<input type="radio" class="rdomcqans" name="qug2" value="3"></label>
<label class="form-radio form-normal active form-text">4<input type="radio" class="rdomcqans" name="qug2" value="4"></label>
<!--qug2 = Question Group 2-->
<input type="hidden" id="McqAnswerListArray" name="McqAnswerListArray" />
<script>
$('.rdomcqans').on('change', function () {
var McqAnswerListArray="";
var count = 0;
McqAnswerListArray += "";
$('.rdomcqans:checked').each(function () {
if (count == 0) {
McqAnswerListArray += $(this).attr('name')+ ":" + $(this).val();
} else {
McqAnswerListArray += ",";
McqAnswerListArray += $(this).attr('name') + ":" + $(this).val();
}
count++;
});
$('#McqAnswerListArray').val(McqAnswerListArray);
});
</script>
控制器应该是这样的
您应该使用Newtonsoft.Json导入;
[HttpPost]
public ActionResult MCQ(String McqAnswerListArray) {
string[] Jsonn = mcm.McqAnswerListArray.Split(',');
for (int i = 0; i < Jsonn.Count(); i++) {
string s = Jsonn[i];
string[] obj = s.Split(':');
string strqid = obj[0].ToString();
int qid = Convert.ToInt32(strqid.Substring(3, strqid.Length - 3));//Get Question Group Number
int Answer = Convert.ToInt32(obj[1].ToString());//Get Group Answer
//You Can save Your answer bellow
}
}