I'm making a simple quiz in ASP.NET MVC4. This is my first application so I'm really new to this.
The problem is, when I display @Viewbag.ans1 (or ans2 etc.), nothing displays. I've done a lot of searching, but I think I may have more than one problem.
Per asawyer's question, the reason I didn't use the Model was that I figured I didn't need a database to keep track of the user's answers and that Viewbag would suffice for that (not sure if I figured right though). I am using it for something else though (the quiz answers).
Here is my Results View. The @Viewbag.ans1 displays nothing on output.:
@{
ViewBag.Title = "Results";
}
<h2>Results</h2>
<p># Correct: 9/10</p> <!--hardcoded, will change later-->
<p>Grade: A </p> <!--hardcoded, will change later-->
<p>Your Answers: </p>
<p>1. @ViewBag.Ans1</p>
<p>2. @ViewBag.Ans2 </p>
<p>3. @Html.Raw(ViewBag.Ans3)</p> <!--Trying a different way here-->
<p>4. @Html.Raw(ViewBag.Ans4)</p> <!--Here too-->
<p>5. @Html.Raw(ViewBag.Ans5)</p> <!--Here too-->
<p>6. @ViewBag.Ans6</p>
<p>7. @ViewBag.Ans7</p>
<p>8. @ViewBag.Ans8</p>
<p>9. @ViewBag.Ans9</p>
<p>10. @ViewBag.Ans10</p>
<div>
@Html.ActionLink("Retry the test?", "Index")
</div>
Here is my Question_1 View. I'm only showing this because this is where I'm setting my Answer variable in my @Html.ActionLink below:
@{
ViewBag.Title = "Question_1";
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
}
<ol class="round">
<li class="one">
What is the name of this website?
</li>
</ol>
@Html.ActionLink("Yes", "Question_2", new{ Answer = true} )
@Html.ActionLink("No", "Question_2", new{ Answer = false} )
Here is my Quiz_Controller:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
namespace Matts_Quiz.Controllers
{
public class QuizController : Controller
{
//
// GET: /Index/
public ActionResult Index()
{
return View();
}
//
// GET: /Question_1/
public ActionResult Question_1()
{
return View();
}
//
// GET: /Question_2/
public ActionResult Question_2(bool Answer)
{
if (Answer == true)
{
ViewBag.Ans1 = "Y";
}
else if (Answer == false)
{
ViewBag.Ans1 = "N";
}
else
{
ViewBag.Ans1 = "Miss";
}
return View();
}
(...etc...)