【发布时间】:2021-04-09 11:03:40
【问题描述】:
下面是示例。
我正在尝试在 Next/Previous 上显示问题。
以下是 Blazor 网页。
其中有 C# 代码和 HTML。
@page "/Quiz"
<table class="table">
<thead>
<tr>
<th>
<h3>Question : @currentCount.ToString()</h3>
</th>
</tr>
</thead>
<tbody>
@foreach (var question in exam.Questions.Where(x => x.Id == currentCount.ToString()))
{
<tr>
<th>
<h3>@question.Text</h3>
</th>
</tr>
@foreach (var choice in question.Choices)
{
<tr>
<th>
<label>
<input type="radio" name="@question.Id" value="@question.Id" />
@choice.Text
</label>
</th>
</tr>
}
}
</tbody>
</table>
<button class="btn btn-primary" @onclick="NextQuestion">Next</button>
<button class="btn btn-primary" @onclick="PreviousQuestion">Previous</button>
@code {
Model.Exam exam = new Model.Exam();
private int currentCount = 1;
protected override void OnInitialized()
{
exam.Id = "1";
exam.Questions = new List<Model.Question>();
Model.Question question1 = new Model.Question();
question1.Id = "1";
question1.Text = "Which data type is used to create a variable that should store text?";
Model.Choice choice1 = new Model.Choice();
choice1.Id = "1";
choice1.Text = "Txt";
Model.Choice choice2 = new Model.Choice();
choice2.Id = "2";
choice2.Text = "str";
Model.Choice choice3 = new Model.Choice();
choice3.Id = "3";
choice3.Text = "myString";
Model.Choice choice4 = new Model.Choice();
choice4.Id = "4";
choice4.Text = "string";
question1.Choices = new List<Model.Choice>() { choice1, choice2, choice3, choice4 };
exam.Questions.Add(question1);
Model.Question question2 = new Model.Question();
question2.Id = "2";
question2.Text = "Which property can be used to find the length of a string?";
choice1 = new Model.Choice();
choice1.Id = "1";
choice1.Text = "getLength()";
choice2 = new Model.Choice();
choice2.Id = "2";
choice2.Text = "length";
choice3 = new Model.Choice();
choice3.Id = "3";
choice3.Text = "Length";
choice4 = new Model.Choice();
choice4.Id = "4";
choice4.Text = "length()";
question2.Choices = new List<Model.Choice> { choice1, choice2, choice3, choice4 };
exam.Questions.Add(question2);
}
private void NextQuestion()
{
currentCount++;
}
private void PreviousQuestion()
{
currentCount--;
}
}
当我尝试在单页中显示所有问题时。它正在工作。
我为单选按钮使用了不同的名称。
可能是什么问题。我错过了什么吗?
我正在做的设计是正确的吗?
我正在使用 Blazor Web 程序集。
【问题讨论】:
-
嗨。你能澄清一下吗?应该同时显示多少个问题?在您的问题中,您使用了复数形式,但您的源代码使用
Id字段表明只能同时显示一个问题。它是否正确?您想实现将“选择”写回考试/问题/选择模型吗?您是否在模型中设计了一个属性来记住选择了哪个选项? -
是的,我有一个 Selectedanswer 的属性。目前我正在显示一个问题,当用户单击 Next -> 时会出现 Next question。
标签: c# html blazor webassembly blazor-webassembly