where to store temporary data in MVC 2.0 project
- by StuffHappens
Hello!
I'm starting to learn MVC 2.0 and I'm trying to create a site with a quiz: user is asked a question and given several options of answer. If he chooses the right answer he gets some points, if he doesn't, he looses them.
I tried to do this the following way
public class HomeController : Controller
{
private ITaskGenerator taskGenerator = new TaskGenerator();
private string correctAnswer;
public ActionResult Index()
{
var task = taskGenerator .GenerateTask();
ViewData["Task"] = task.Task;
ViewData["Options"] = task.Options;
correctAnswer= task.CorrectAnswer;
return View();
}
public ActionResult Answer(string id)
{
if (id == correctAnswer)
return View("Correct")
return View("Incorrect");
}
}
But I have a problem: when user answers the cotroller class is recreated and I loose correct answer. So what is the best place to store correct answer? Should I create a static class for this purpose?
Thanks for your help!