I'm new to programming. I have basic understanding of Html, Css, C#, SQL queries, Entity Framework and ASP.Net-MVC. I'm trying to make a small time table test in ASP.Net-MVC.
The purpose of this application is to ask 10 questions, one question at a time and each time a question is attempted user will click on next button that will show next set of question. User need to attempt all 10 questions. Once reached it will tell show a message e.g. "Congratulations You have answered 7 questions From 10".
I want to know if it is possible to get data from html form and then store it in C# variable in ASP.Net MVC View. Below is what I have tried so far:
Question View
@{
Random rnd = new Random();
int num1 = rnd.Next(13);
int num2 = rnd.Next(13);
int answer = num1 * num2;
int wrng1 = answer - 2;
int wrng2 = answer + 10;
int wrng3 = answer + 4;
int QAnswered = 0;
}
<div style="text-align:center;">
<h1>Questions Answered: @QAnswered</h1>
<!--Writes the Question-->
<h2 style="font-size: 150px; font-weight:900;">@num1 X @num2 = ?</h2>
<!--Buttons for Answers in form-->
<form action="/Home/CheckAnswer" method="post">
<input type="radio" name="UserAnswer" value="@answer" style="margin-right: 10px; width: 50px; height: 50px;" /><label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@answer</label>
<input type="radio" name="UserAnswer" value="@wrng1" style="margin-right: 10px; width: 50px; height: 50px;" /><label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@wrng1</label>
<br /><br /><br />
<input type="radio" name="UserAnswer" value="@wrng2" style="margin-right: 10px; width: 50px; height: 50px;" /><label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@wrng2</label>
<input type="radio" name="UserAnswer" value="@wrng3" style="margin-right: 10px; width: 50px; height: 50px;" /><label style="color:white; font-size: 75px; width: 150px; height: 100px; border:1px solid black; background-color: cornflowerblue;">@wrng3</label>
<br /><br />
<input type="hidden" name="CorrectAnswer" value="@answer" />
<input type="hidden" name="QAnswered" value="@QAnswered" />
<input class="btn btn-primary" type="submit" value="Next" style="font-size: 50px; width: 150px; height: 75px;" />
</form>
and this is the code for the Actions in home controller:
public ActionResult Question()
{
return View();
}
[HttpPost]
public ActionResult CheckAnswer(int UserAnswer, int CorrectAnswer, int QAnswered)
{
int TrueAnswers = 0;
int FalseAnswers = 0;
//Check whether UserAnswer is true or false and increment values of them
if (UserAnswer == CorrectAnswer)
{
TrueAnswers += 1;
QAnswered += 1;
}
else if (UserAnswer != CorrectAnswer)
{
FalseAnswers += 1;
QAnswered += 1;
}
if (QAnswered < 10)
{
return RedirectToAction("Question");
}
else
{
return RedirectToAction("Index");
}
}
The thing is I don't want to make CheckAnswer action return content instead I want it to store the UserAnswer and check if it was correct then it will (somehow) store it as a true and vice versa and and after 10 questions (whether if it was right or wrong) I want it to display another View Which Will say something like "Congratulations you have answered 7 out of 10 questions correctly".
I don't know how will I do it so I hope anyone Will Help Me.