1

I'm trying to make a quiz making and taking site. Currently you can input a quiz, questions, and answers. Now I'm trying to write the quiz taking part (any suggestions on how to do this would help!). I'm following the model here and my generated output file looks like this. Now, when I copy the code to its own file and run it, it runs correctly. It makes sure all questions are answered, and it will tell you for multiple questions which one is correct.

However when I load up the html raw generated javascript from the website itself, it only checks to see if you have answered one of the questions and not the other, and it only displays the results of one answer when you click finish. If you answer both questions wrong it only shows 1 answer as wrong instead of 2 (for my example).

Here is what my css looks like. Here is the html raw portion of the code.

I am just not sure why I am getting problems with javascript generated by @Html.Raw. If there is another way to write a quiz taker I am all ears as well.

Thanks.

edit:

Again, if I copy/paste code in its own file it works, but rendered from MVC it doesn't work.

takequiz cshtml

takequiz controller

1 Answer 1

1

Honestly, I can't figure out why you use @Html.Raw() on each script line. The fact that you put all this in a <script> tag makes this completely unnecessary, and it's very difficult to read. Without it, you can still use Razor syntax and C# variables if needed.

My guess is that this is partially what's causing all the issues, perhaps you are missing something because it's so difficult to see what might be wrong.

Either before or after you cleaned up the code from the Html.Raw's, I recommend you to check what the script looks like once the element is rendered on a page, and compare it to the working code from the separate script file.

If you still can't get it working, please show us your cleaned up code, and your separate code file which was working, and perhaps me or someone else can take a closer look.

EDIT: I tried pasting the code and markup from your pastebin in the comments, into a simple html file with Notepad++. Saved it and checked if I got any errors. I did get an error: $ is not defined which usually means that either the library isn't loaded in time, or it isn't loaded at all. So, it seems to me that the issue lies in the fact that you put the reference to the jQuery library after your quiz script.

Before your quiz script, you have this line:

<script src="jquery.js"></script>

I'm not sure if it has any effect, since you have a more specific reference in the bottom of the page:

<script src="/Scripts/jquery-1.7.1.js"></script>

What I did was switching places between the two, so that the 1.7.1.js reference is placed before the quiz script, and then I removed the other one completely. And so the script works. I also put the code into an MVC view. While I didn't really have time to set up all the controller stuff and the foreach that you had initially, the connection between the controller and the view is what I would check next if what I wrote above didn't help.

As I said in the comments though, I'm quite sure you'll find something of use if you check the consoles in your browsers developer tools.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks for the reply. The problem is my working file is the exact same code as what is generated by MVC. I got this (pastebin.com/0WRn0CES) file by literally copying/pasting the code and chaning the style.css and jquery.js lines to match my local directory. There is no programmatic problem with the code as far as I can tell, I cleaned it up anyway and I still have the same problems. Something about the way MVC renders the code/html is making things sour. If there is some better way I can describe my problem let me know.
Hi again! Sorry for not responding earlier. Have been checking this from time to time, probably too tired because I still can't figure it out. But thank you for cleaning up the code! Much easier for me to read. First: Have you checked if you get any errors in the console in either chromes developer tools or in firebug for firefox? Anyway, I have a new guess of what it can be, but I'm going to edit my answer to avoid a wall of text here (probably too late for that).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.