I`m making a web app for online exams and after i write the question and the answers, i want to select the right answer by checking a checkbox. [The right answer][1] will have the same value as the checked answer but i do not know how to make it appear in the [question list][2] or in the [database][3].
Model variables:
public string question{get;set;}
public string asnwer1{get;set;}
public string answer2{get;set;}
public string answer3{get;set;}
public string answer4{get;set;}
public string rightanswer{get;set;}
Create question page cshtml:
Intrebare = Question | Optiune = Answer | Raspuns = Right Answer.
<div class="form-group">
<label asp-for="AplicatieIntrebare.Intrebare" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Intrebare" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Intrebare" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune1" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune1" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune1" class="text-danger"></span>
<input id="Checkbox1" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune2" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune2" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune2" class="text-danger"></span>
<input id="Checkbox2" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune3" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune3" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune3" class="text-danger"></span>
<input id="Checkbox3" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune4" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune4" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune4" class="text-danger"></span>
<input id="Checkbox4" type="checkbox" />
</div>
Thank you! [1]:https://i.sstatic.net/MRUqr.png [2]:https://i.sstatic.net/sgnst.png [3]:https://i.sstatic.net/ztIIc.png
If you want to bind Raspuns when the checkbox is checked,you can add a hidden input with asp-for="AplicatieIntrebare.Raspuns"
,and before posting form,iterate div with class="form-group"
,then check if the check box in the div is checked.If so,set the hidden input value with the input's value in the div.Here is a demo:
cshtml:
<form method="post" id="myform">
<div class="form-group">
<label asp-for="AplicatieIntrebare.Intrebare" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Intrebare" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Intrebare" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune1" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune1" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune1" class="text-danger"></span>
<input id="Checkbox1" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune2" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune2" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune2" class="text-danger"></span>
<input id="Checkbox2" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune3" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune3" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune3" class="text-danger"></span>
<input id="Checkbox3" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune4" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune4" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune4" class="text-danger"></span>
<input id="Checkbox4" type="checkbox" />
</div>
<input type="submit" value="submit" />
<input asp-for="AplicatieIntrebare.Raspuns" hidden />
</form>
@section scripts {
<script>
$("#myform").submit(function () {
$(".form-group").each(function (index, element) {
if ($(element).children("input[type='checkbox']").prop("checked")) {
$("#AplicatieIntrebare_Raspuns").attr("value", $(element).children("input[type='text']").val());
}
})
})
</script>
}
cshtml.cs:
[BindProperty]
public AplicatieIntrebare AplicatieIntrebare { get; set; }
public void OnGet()
{
AplicatieIntrebare = new AplicatieIntrebare { Intrebare = "What's my dog's name?", Optiune1 = "Jerry", Optiune2 = "Spot", Optiune3 = "Rex", Optiune4 = "Max" };
}
public void OnPost()
{
}
result: