我是编程新手。我对HTML,Css,C#,SQL查询,实体框架和ASP.Net-MVC有基本的了解。我正在尝试在ASP.Net-MVC中进行小型时间表测试。
此应用程序的目的是要问10个问题,一次问一个问题,并且每次尝试提问时,用户都将单击“下一步”按钮,该按钮将显示下一组问题。用户需要尝试所有10个问题。到达后,它会告诉显示一条消息,例如“恭喜,您已经回答了10个问题中的7个。”
我想知道是否有可能从html表单获取数据,然后将其存储在ASP.Net MVC视图的C#变量中。以下是到目前为止我尝试过的事情:
问题视图
@{
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>
这是家庭控制器中“动作”的代码:
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");
}
}
问题是我不想让CheckAnswer动作返回内容,而是希望它存储UserAnswer并检查它是否正确,然后它将(以某种方式)存储为true,反之亦然,并在10个问题之后(无论是对还是错)我希望它显示另一个视图,该视图将显示类似“恭喜,您已正确回答10个问题中的7个”。
我不知道该怎么做,所以希望有人能帮助我。
答案 0 :(得分:0)
return Content(“...”)
仅用于返回 TEXT-Plain FORMAT值
如果要在提交表单后执行其他操作,则应输入return RedirectToAction(“YOUR-ACTION”)
更新
控制器:
[HttpGet]
public ActionResult Question()
{
Random rnd = new Random();
int num1 = rnd.Next(13);
int num2 = rnd.Next(13);
return View(new QuestionViewModel
{
QuestionPart1 = num1,
QuestionPart2 = num2,
});
}
[HttpPost]
public ActionResult Question(QuestionViewModel model)
{
//Check whether UserAnswer is true or false and increment values of them
if (model.UserAnswer == (model.QuestionPart1 * model.QuestionPart2))
{
if (Session["QAnswered"] == null)
Session["QAnswered"] = 1;
else
Session["QAnswered"] = int.Parse(Session["QAnswered"].ToString()) + 1;
}
else
{
if (Session["Failed"] == null)
Session["Failed"] = 1;
else
Session["Failed"] = int.Parse(Session["Failed"].ToString()) + 1;
}
if (Session["QAnswered"] != null && int.Parse(Session["QAnswered"].ToString()) == 10)
{
// successful
return RedirectToAction("Congratulation");
}
else
{
// failed
return RedirectToAction("Question");
}
}
查看:
@model WebApplication2.Models.QuestionViewModel
@{
int answer = Model.QuestionPart1 * Model.QuestionPart2;
int wrng1 = answer - 2;
int wrng2 = answer + 10;
int wrng3 = answer + 4;
var answers = new List<int> { answer, wrng1, wrng2, wrng3 };
Random rnd = new Random();
answers = answers.OrderBy(x => rnd.Next()).ToList();
}
<div style="text-align:center;">
<h1>Questions Answered: @Session["QAnswered"]</h1>
@if (int.Parse(Session["QAnswered"].ToString()) >= 7){
<text>
Congratulations, you've answered 7 of 10.
</text>
}
<!--Writes the Question-->
<h2 style="font-size: 150px; font-weight:900;">@Model.QuestionPart1 X @Model.QuestionPart2 = ?</h2>
<!--Buttons for Answers in form-->
@using (Html.BeginForm("Question", "Home", FormMethod.Post))
{
for (int index = 0; index < 4; index++)
{
var choice = answers[index];
<input type="radio" id="UserAnswer" name="UserAnswer" value="@choice" 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;">@choice</label>
<br />
}
@Html.HiddenFor(model => model.QuestionPart2)
@Html.HiddenFor(model => model.QuestionPart1)
<input class="btn btn-primary" type="submit" value="Next" style="font-size: 50px; width: 150px; height: 75px;" />
}
</div>
型号:
public class QuestionViewModel
{
public int QuestionPart1 { get; set; }
public int QuestionPart2 { get; set; }
public int UserAnswer { get; set; }
}