我正在开发投票页面机制。在这里,我将有问题列表,并且针对每个问题我有 3 个选项(我使用单选按钮)。我附上了我的视图和控制器方法。我将值正确保存到数据库,但我的问题是我能够选择使用单选按钮的多个选项。我想确保,如果为某个问题选择了一个选项,则必须自动取消选择其他选项,这对我来说不会发生。
我的观点 :
@using (Html.BeginForm())
{
<div>
@foreach (var a in ViewBag.Questions)
{
<h4>@a.Questions</h4>
<div>
@foreach (var b in Model)
{
if (b.QuestionsID == a.id)
{
@Html.RadioButton(b.AnswersOptions,
new {Answerid= b.id, Questionid=a.id })
@b.AnswersOptions
}
}
</div>
}
</div>
<br/>
<div >
<input type="submit" value="Vote Now!!"
onclick="return confirm('Are you sure you want to
submit your choices?');"/>
</div>
}
我的控制器:
public ActionResult VotingResult_Post(FormCollection resultcollection)
{
int resultcollectionCount = resultcollection.Count;
if (resultcollectionCount == CountofQuestionsDisplayed)
{
for (int i = 0; i < resultcollectionCount; i++)
{
string SelectedIDArray = resultcollection[i];
string SelectedAnswerIDValue = GetValue("Answerid", SelectedIDArray);
string SelectedQuestionID = GetValue("Questionid", SelectedIDArray);
InsertUsersReponses(SelectedQuestionID, SelectedAnswerIDValue);
}
}
List<Voting_Questions> QuesList = PopulateQuestions();
ViewBag.Questions = QuesList;
List<Voting_Answers> Answers = aobj.Voting_Answers.ToList();
return View(Answers);
}