- The user goes to the quiz page to take the quiz
- The quiz has a question and a list of answers choices to choose from.
- When the user clicks on the next button , a new question is loaded dynamically.
- When the questions are completed the user is sent to a page showing his or her results.
Some ASP.Net MVC stuff
I used code first method by making poco classes and scaffolded all my controllers and i got all my CRUD methods an views for the controllers I choosed. Asp.net MVC Entity framework now the default database object relational mapper for the framework.To know more about Code first and the new scaffolding in Asp.net MVC check out this link.Scaffolding and Entity framework code first The next thing for me was to decide of what real life objects can be used and how they are connected.In this case- I will need a to represent the question, the answer, and the answer choices.
- I needed one object to bring all these other object together and that is the Quiz object .
- I needed another object to manage the quiz since I decided to store the users answers in the memory.
To manage the quiz I use the Singleton pattern .The is only one quiz in the memory when you start until you finish. The quiz manager manages all events related to the quiz in the memory .I did not want to use the session object because wanted an optimal and a modular solution for the manager. Here is the code
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MyQuiz.Abstract; namespace MyQuiz.Models { public class QuizManager { static QuizManager instance; private QuizContext db = new QuizContext(); int questionId = 1; public bool IsComplete = false; public Quiz quiz; private QuizManager() { quiz = new Quiz(); quiz.StartTime = DateTime.Now; quiz.QuizId = 1; quiz.Score = 0 ; } public static QuizManager Instance { get { if (instance == null) instance = new QuizManager(); return instance; } } public Question LoadQuiz() { var question = db.Questions.Find(questionId); return question; } public void SaveAnswer(string answers) { var question = db.Questions.Include("Answers").Where(x => x.QuestionId == questionId).Single(); if (question.Answers.AnswerText == answers) quiz.Score++; } public bool MoveToNextQuestion() { bool canMove = false; if ( db.Questions.Count() > questionId) { questionId++; canMove = true; } return canMove; } public bool PreviosQuestion() { bool canMove = false; if (questionId > 1) { questionId--; canMove = true; } return canMove; } } }
The home controller that sends the quiz to the view was implemented this way
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MyQuiz.Models; namespace MyQuiz.Controllers { public class HomeController : Controller { public ActionResult Index() { var question = QuizManager.Instance.LoadQuiz(); return View(question); } [HttpPost] public ActionResult Index(string answer, int questionId) { if(QuizManager.Instance.IsComplete) // Prevent score increase when quix has been completed return RedirectToAction("ShowResults"); QuizManager.Instance.SaveAnswer(answer); if (QuizManager.Instance.MoveToNextQuestion()) { var question = QuizManager.Instance.LoadQuiz(); return View(question); } QuizManager.Instance.IsComplete = true; return RedirectToAction("ShowResults"); } public ActionResult About() { return View(); } public ActionResult ShowResults() { return View(QuizManager.Instance.quiz); } } }
The View of the quiz . I use the JQuery live(---) function that allows for asynchronous loading and event handling on the page . After getting the response from the controller I simply replace the quiz div area with new questions using the replaceWith(---) function in JQuery. The process continuous until the user answers the last question .
@model MyQuiz.Models.Question @{ ViewBag.Title = "Index"; }@using (Html.BeginForm("Index", "Home")) {
@Model.QuestionText
@Html.HiddenFor(x => x.QuestionId)
@foreach (var choice in Model.AnswerChoices) { }@Html.RadioButton("answer", @choice.Choices) @choice.Choices
}
The result page just displays the results of the quiz .Here is the source
@model MyQuiz.Models.Quiz @{ ViewBag.Title = "ShowResults"; }Here is a screen shot of the final applicationShowResults
Your total score is @Model.Score
- There is more to add like scaffolding repositories.
- Use Dependency injection to reduce coupling of objects
Here is the source code if want to play with it. I moved the source code to Github since some readers could not access the source code on google drive. I took advantage to play with the new Github for windows which I find intresting since i have always used Apache Subversion, svn.
I have made some improvement on the code like moving it to MVC4 and you can see the final application running here MVC Quiz Application on AppHarbor (I love these guys)
Happy programming !
Thank you for this. Any chance you could post the source somewhere other than Google Docs? It's blocked at my work so I can't see the source. Thanks!
ReplyDeleteI will do that.I have been thinking about github or codeplex.I will try to push the code to one of these online repositories
ReplyDeleteThank You for sharing. At my work, I am not allowed to install any thing. I am starting to work on a standalone quiz module and your project looks great. If publishing on codeplex is a lot of work, can you e-mail me a zip file.
ReplyDeleteThanks!
Chandrika
ChandrikaHarathi@yahoo.com
Thanks for sharing this..Please do let us know where can we find this code.
ReplyDeleteThanks,
Vinod
https://github.com/ngalakvist/Quiz/tree/master/MyQuiz-2012-07-01/MyQuiz
ReplyDeleteThis is great, nicely broken into classes and very well thought out entirely. Thanks for sharing!
ReplyDeleteThanks for your feedback.
DeleteHi downloaded all files from https://github.com/ngalakvist/Quiz/tree/master/MyQuiz-2012-07-01/MyQuiz
ReplyDeletebut what about database? how do i generate it ? i'm using EF4
please guide thanks
I have added a file to generate the database for you when you download the application. First you need to install Sql management studio express on your machine.
ReplyDeleteThanks a ton,
ReplyDeleteI have generated database and tables successfully, also added Model1.edmx file and generated classes using POCO generator in Models folder.
Now we don't need to use `QuizContext.cs` class ? as we have generated using POCO ?
Also we need to made some changes into `QuizeManager.cs` like
public class QuizManager:IQuizManager
{
public Entities1 db = new Entities1();
// public QuizContextdb = new QuizContext();
please correct me ?
I'm getting many error as POCO giving us `ObjectSet` instead `DataSet` ?
Error :
// GET: /Quiz/Edit/5
public ActionResult Edit(int id)
{
Quiz quiz = db.Quizs.Find(id);
return View(quiz);
}
Does not contain definition Find()
I wrote this app a while ago. Some of the software like EF and MVC framework have new versions now. Everything cannot be the same like when i wrote it.I will advice you to use the concept and the model i used to create your own app. That's what i will do especially if you are using VS2012.
ReplyDeletethanks I made changes successfully :), may i know why this table is there in you db 'EdmMetadata' ? please clarify the use ?
ReplyDeleteDo a search for EdmMetadata in google and click on the first link.
ReplyDeletethanks may i know why we have columns like starttime, duration etc into table dbo.Quizs as it is haven't codded anywhere?
ReplyDeleteI wanted to have a timer for each question and a displayed count down.You can implement that if you like and improve on the application. After say 10 seconds the next question is shown.
ReplyDeleteHi there
ReplyDeleteThanks for sharing this.
I can't locate the create table scripts or the MDF files on the github repository. Can you please advice?
Oh great. Just awesome tutorial. I have also created a script which dynamically receives the data from JSON and shows it one-by-one. The score will also be calculated side by side.
ReplyDeleteYou can check this example to know more.
hello Ngala. I am unable to attach or onfigure database from this application. Can you please guide me.. if possible can you please attach a DB script so that I can continue.
ReplyDeleteI wanna pull data from db to a list, and then once time click Next, I get one question from the list. How can I do that? Please help me
ReplyDeleteHello. If you want to get all the questions to a list and get one question at a time from there, Ask yourself how you are going to save state if all the data is gotten and stored on a list .I do not know why you want to do this but anyway you can proceed by.
DeleteGet all the questions from the DB
Save them to a list or
Create a queue and add the questions from it and pop out questions from it.
Good luck !
Hi Ngala
ReplyDeleteCan you please provide the sample data?
Best Regards,
Damo
Hi,
ReplyDeleteI need the previous button functionality
Hi ,
ReplyDeleteI need the previous button functionality and code
Opening this on vs2010 gives an error - doesn't support this installation. What to do!! Can u help
ReplyDeleteHello
ReplyDeleteCan you provide details code which is writtten in view
ReplyDeleteHere is the link to the source code of this application. It on Github so can get it and make it run on ýour machine. See that you have the database well configured rightly.
ReplyDeletehttps://github.com/ngalakvist/Quiz/tree/master/MyQuiz-2012-07-01/MyQuiz/MyQuiz
regards
Ngala Talla
Please talk me how download source code and how run database? Thanks you!
ReplyDeleteNice Project.
ReplyDeletePlease send me your source code. My email is gianguyen4391dn@gmail.com.
Thanks and Best Regards
Great & useful Article
ReplyDeleteOnline MVC Training
Online MVC Training from India
MVC 4 Training
MVC 5 Training
MVC 6 Training
Online MVC Training
please send me code i will be very thankful to you
ReplyDeleterahimiit44@gmail.com
ReplyDeleterahimiit44@gmail.com
ReplyDeleteplease send me code i will be very thankful to you
ReplyDeleteYou have created a very good application.Can you please share the db of this application..i have created but i am getting problems
ReplyDeleteThanks for your comment .Everything is on github. The model you see reflects the database . From the model the database tables and keys are created using Entityframework. Its a model driven development application
ReplyDeleteFirst off, thank you so much for this example. It's very well thought out and built accordingly. I'm having a run-time error when I run it (I'm very new to MVC but have some experience with C#). The error is a null reference of an object instance from the Home/Index page where the "@Model.QuestionText" is used. I've been very careful to mimic your code, but I'd really appreciate some help figuring it out if you have time. I can send you my project files if you want? Please let me know. Thanks again! (my email should be on my google profile)
ReplyDeleteNevermind, I believe I have it figured out now. Thanks. I didn't think to look at my QuestionId number in my DB. It was "2" instead of "1" (QuizManager.cs).
DeleteThanks for your comments and good to know that you figured it out .
ReplyDeleteI am using visual studio 2017 source code gives error. If i run your code its not working... how to fix please help me
ReplyDeleteDear Ngala Sir, I am looking for a online quiz application in asp.net with sql db supporting in back-end data. So i request you to please help me in this concern.
ReplyDeleteReally impressed! Everything is very open and very clear clarification of issues. It contains truly facts. Your website is very valuable. Thanks for sharing. עלויות בנייה פרטית
ReplyDeleteI admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. www.dynamicmarketing.sg/seo-consultant-singapore
ReplyDeleteApplying for an Operators licence To apply for a new operator licence you will need to complete a form called "GV79" fokuszpalyazat.hu form. You can read the associated GV79 (G) guidance notes to help you with the application.
ReplyDeleteIt sounds promising. I fixed all compilation errors but as "MyQuiz database" in github only generated the database, not the tables, it caused the unhandled exception at line #37 ("var question = db.Questions.Find(questionId);" in QuizManager.cs.
ReplyDeleteYour business can reap myriad benefits by getting an Android application developed. You can take the advantage of Android's huge market. There are many highly skilled developers available to build tailored and cost-effective apps. The built-in functionality of Android devices also supports developers in creating feature-rich applications. Android apps will thus help your business to sustain the competition by quickening its time-to-market and enhancing the ROI. Mythical Hack for sweatcoin
ReplyDelete