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!
There are many different ways to persist data across multiple requests.
to name a few. The simplest of these is probably a view state implementation. You can roll your own like this
This input will get reposted in the next submission, so you can keep track of the value.