I am making a quiz/exam page that uses a Question component. The questions contain answers with either a checkbox or radio selection and a submit button. I need to be able to compare the selected answer(s) to the correct answer when a user clicks submit (or some other button type), but I am not sure how to get the value of the input inside of my @code block for processing.
I've messed around with @bind-value but it doesn't seem to relate to the button click at all and I only want the value from one answer, the selected one.
So far using a "submit" button doesn't seem to be the right route because it does a POST request, which is not the behavior I'm looking for. I just want the button to trigger processing in the component.
Here is my component
<form>
<div class="q-card">
<h3>@Question.QuestionText</h3>
@foreach (var option in Question.Options)
{
<div>
<input type="@Question.InputType" id="@answer" name="@Question.Id" value="@answer"/>
<label for="@answer">@answer</label>
</div>
}
<div class="q-controls">
<button type="submit"@onsubmit="@OnSubmit">Submit</button>
</div>
</div>
</form>
and my @code block
@code {
public string answer;
public void OnSubmit()
{
System.Console.WriteLine(answer);
}
}
I want the value from the selection to end up in the variable answer when I click Submit. I realize if there are multiple answers (checkbox) I'll need to change this variable, but I'll get to that later.