ASP.NET MVC Опрос с множественным выбором
Я создаю простой опрос с использованием ASP.NET MVC. Администратор будет создавать вопросы, отвечающие только 3 вариантами - полностью согласен, несколько согласен, категорически не согласен) через представление "Создать".
Затем пользователь должен будет выбрать свой ответ с помощью радиокнопки.
- Как мне добавить это на мой взгляд?
В настоящее время так выглядит мой взгляд (я знаю, что это все еще неправильно, так как я все еще пытаюсь понять, как сделать это правильно):
<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
]
Есть ли примеры или идеи, которым я могу следовать, чтобы достичь этого? Спасибо за любую помощь.
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>
Я надеюсь, что вы поможете это
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
}
}