0

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...)
  • 2
    Why are you not using models? Thats what the M in MVC stands for... – asawyer Apr 05 '13 at 18:13
  • Yeah, I'm using a model for something else (the quiz answers). 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) . – Mathew Cowdery Apr 05 '13 at 18:25
  • I realize you may be trying to keep the amount of posted code to a minimum, so I'm forced to view what is posted very literally. Could you try unconditionally settting the ViewBag value in the `Question_1` controller. Add a line to that controller such as: `ViewBag.Ans1 = "Miss";` and then let us know if you see something in the view when it executes. – David Tansey Apr 05 '13 at 18:53
  • After reading your post again I think I understand better overall what it is you are trying to accomplish. `ViewBag` **will NOT satisfy** your requirements because the 'lifetime' of its values are quite short. Please consider the following [SO](http://stackoverflow.com/questions/6858723/lifetime-of-viewbag-elements-in-asp-net-mvc3) post about the lifetime of ViewBag values and then consider using a ViewModel. You still need some way to 'keep' the values the user has already entered until they are finished. You could Session for this purpose if you do not want to use the DB. – David Tansey Apr 05 '13 at 19:16

0 Answers0