ASP.NET MVC 多项选择调查
ASP.NET MVC Multiple choice survey
我正在使用 ASP.NET MVC 创建一个简单的调查。
管理员将创建仅可通过 3 个选项回答的问题 - 非常同意、有点同意、非常不同意)通过创建视图。
然后用户必须使用单选按钮select他们的答案。
- 我应该如何在我的视图中添加它?
目前,这是我的观点(我知道这仍然是错误的,因为我仍在努力理解如何正确地做到这一点):
<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>
- 如何获得用户对每个问题的 selected 答案并将其保存到我的数据库中?
我的表格如下:
tblTestProper: [QuestionID
, UserID
, Answer
]
tbl问题: [QuestionID
, Question
]
有没有我可以遵循的示例或想法来实现这一目标?感谢您的帮助。
希望对你有所帮助
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>
控制器应该是这样的
您应该导入 using 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
}
}
<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>
我正在使用 ASP.NET MVC 创建一个简单的调查。 管理员将创建仅可通过 3 个选项回答的问题 - 非常同意、有点同意、非常不同意)通过创建视图。
然后用户必须使用单选按钮select他们的答案。
- 我应该如何在我的视图中添加它?
目前,这是我的观点(我知道这仍然是错误的,因为我仍在努力理解如何正确地做到这一点):
<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>
- 如何获得用户对每个问题的 selected 答案并将其保存到我的数据库中?
我的表格如下:
tblTestProper: [QuestionID
, UserID
, Answer
]
tbl问题: [QuestionID
, Question
]
有没有我可以遵循的示例或想法来实现这一目标?感谢您的帮助。
希望对你有所帮助
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>
控制器应该是这样的
您应该导入 using 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
}
}
<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>