I'm trying to create a test mechanism, after ticking the right option, I want to save this option to the db via my model, but I can't map the value from the JavaScript to my model. How can I do that?
@model Exam.Models.ViewModels.QuestionMakerViewModel
<table id="s1">
<tbody>
<form asp-action="QuestionMaker" asp-controller="Teacher" method="post">
<tr>
<div >
<label for="exampleFormControlTextarea1" >Question 1</label>
<textarea [email protected] id="exampleFormControlTextarea1" rows="10"></textarea>
</div>
</tr>
<tr>
<td colspan="2">
<table >
<tbody>
<tr>
<div >
<button id="A" type="button" onclick="clickFunc(this.id)">A</button>
<input type="text" [email protected] placeholder=""
aria-label="Example text with button addon" aria-describedby="button-addon1">
</div>
</tr>
<tr>
<div >
<button type="button" id="B" onclick="clickFunc(this.id)">B</button>
<input type="text" [email protected] placeholder=""
aria-label="Example text with button addon" aria-describedby="button-addon1">
</div>
</tr>
<tr>
<div >
<button type="button" id="C" onclick="clickFunc(this.id)">C</button>
<input type="text" [email protected] placeholder=""
aria-label="Example text with button addon" aria-describedby="button-addon1">
</div>
</tr>
</tbody>
</table>
<button type="submit" onclick="clickSub()" asp-action="QuestionMaker" asp-controller="Teacher" formmethod="post">Kaydet</button>
</td>
</tr>
</form>
</tbody>
Here I got the results from the button id with javascript
<script type="text/javascript">
function clickFunc(answerId) {
@Model.TrueAnswer=answerId;
}
CodePudding user response:
You can try to use a hidden input to keep the value of @Model.TrueAnswer
,and then when click the button,change the value of the hidden input,and submit the form to action:
@model Exam.Models.ViewModels.QuestionMakerViewModel
<table id="s1">
<tbody>
<form id="myForm" asp-action="QuestionMaker" asp-controller="Teacher" method="post">
<tr>
<div >
<label for="exampleFormControlTextarea1" >Question 1</label>
<textarea [email protected] id="exampleFormControlTextarea1" rows="10"></textarea>
</div>
</tr>
<tr>
<td colspan="2">
<table >
<tbody>
<tr>
<div >
<button id="A" type="button" onclick="clickFunc(this.id)">A</button>
<input type="text" [email protected] placeholder=""
aria-label="Example text with button addon" aria-describedby="button-addon1">
</div>
</tr>
<tr>
<div >
<button type="button" id="B" onclick="clickFunc(this.id)">B</button>
<input type="text" [email protected] placeholder=""
aria-label="Example text with button addon" aria-describedby="button-addon1">
</div>
</tr>
<tr>
<div >
<button type="button" id="C" onclick="clickFunc(this.id)">C</button>
<input type="text" [email protected] placeholder=""
aria-label="Example text with button addon" aria-describedby="button-addon1">
</div>
</tr>
</tbody>
</table>
<input name="TrueAnswer" id="TrueAnswer" hidden />
<button type="submit" onclick="clickSub()" asp-action="QuestionMaker" asp-controller="Teacher" formmethod="post">Kaydet</button>
</td>
</tr>
</form>
</tbody>
js:
function clickFunc(answerId) {
$("#TrueAnswer").val(answerId);
$("#myForm").submit();
}
CodePudding user response:
I got the result by making a small change in the answer you gave, I have been trying for hours, I have solved the problem thanks to you, thank you for your help.
function clickFunc(answerId) {
$("#TrueAnswer").val(answerId);
$("#myFunction").submit();
}
When we submitted the form, it was sending the model to the controller without filling the texts. I got the solution by typing function instead of form.