我有一个视图,旨在显示一个问题和几个代表问题可能答案的单选按钮。每个问题的可能答案数量都会发生变化,因此我需要找到一种动态生成单选按钮的方法。我是 aspx 语法的新手,不太确定如何处理,即如何在下面的 html 中显示我在脚本中创建的单选按钮集合?使用 RadioButtonList 会更好吗?
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<UncleBobWebService.Models.MultipleChoiceQuestion>" %>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
<title>Application</title>
<link href="/Theme/UncleBobTheme.css" rel="Stylesheet" />
<script lang="cs" runat="server">
protected void Page_Load(Object o, EventArgs e)
{
int count = Model.PossibleAnswers.Count;
List<RadioButton> radioButtons = new List<RadioButton>();
for (int i = 0; i < count; ++i)
{
RadioButton button = new RadioButton();
button.ID = "1";
button.GroupName = "answers";
button.Text = Model.PossibleAnswers[i+1].TheAnswer;
button.Checked = false;
radioButtons.Add(button);
}
}
</script>
</head>
<body>
<h2>Question</h2>
<div class="body">
<form id=form1 runat=server action="/ApplicantApply/FormAction">
<div class="box">
<div class="left-justify">
<div><%= Html.DisplayFor(Model => Model.Question) %></div>
<!-- How to display the radiobutton list here instead of below? -->
<input type="radio" id="yesAnswer" name="yes" value="Yes">Yes<br />
<input type="radio" id="noAnswer" name="no" value="No">No<br />
</div>
</div>
<input type="submit" name="submit" value="Previous" />
<input type="submit" name="submit" value="Next" />
</form>
</div>
</body>
</html>