1

I am able to display the questions by id but dont know how to randomly display them for different users and everytime the user logs in a new random combination is initiated. can someone guide?

My Controller:-

public ActionResult Index()
    {
        var question = Quiz.Instance.LoadQuiz();
        return View(question);

    }

    [HttpPost]
    public ActionResult Index(string Opt)
    {
        if (Quiz.Instance.IsComplete)
        {
            return RedirectToAction("ShowResult");
        }
        Quiz.Instance.SaveAnswers(Opt);
        if (Quiz.Instance.MovetoNext())
        {
            var question = Quiz.Instance.LoadQuiz();
            return View(question);
        }
        Quiz.Instance.IsComplete = true;
        return RedirectToAction("ShowResult");
    }`

Quiz repository:

public Question_OptionViewModel LoadQuiz()
    {


        var questions = db.Questions.Find(Q_ID);
        var options = from o in db.Options
                      select o;
        options = options.Where(o => o.Q_Id == Q_ID);

        var viewmodel = new Question_OptionViewModel()
        {
            Question = questions,
            Options = options
        };
        return viewmodel;
    }

    public void SaveAnswers(string answer)
    {
        Attempt at = new Attempt()
        {
            Q_Id = Q_ID,
            Answer = answer, 
            Registration_number = 1312153  
        };
        db.Attempts.Add(at);
        db.SaveChanges();
        var questions = db.Questions.Where(q => q.Q_Id == Q_ID).First();
        if (at.Answer == questions.Correct_Ans)
        {
            result.Score++;
        }
    }

    public bool MovetoNext()
    {
        int questions = db.Questions.Where(q => q.Test_Id == 1).Count();
        bool canmove = false;
        if (questions > Q_ID)
        {
            Q_ID++;
            canmove = true;
        }
        return canmove;
    }

My view:-

    @model OnlineTestSystem.ViewModels.Question_OptionViewModel

@{
  ViewBag.Title = "Quiz";
}

    $("#submitButton").live("click", function () {
    $.get($(this), function (response) {
        $("#quiz").replaceWith($("#quiz", response));


    });
    return false;
});
});

    <div id="quiz" style="text-align:center">
        @using (Html.BeginForm("Index", "Test"))
        {
            <fieldset>


                <legend><h2>@Model.Question.QuestionText</h2></legend>

            <li>
                @foreach (var opt in Model.Options)
                {
                    <ul class="list-inline" style="font-size:17px">
                        @Html.RadioButton("Opt", @opt.Opt) @opt.Opt
                    </ul>
                }
            </li>
            <input class="btn btn-default" type="submit" value="Next" id="submitButton" />
        </fieldset>
        }

    </div>

``

1 Answers1

1

You could just simply randomize your question by using a Shuffle algorithm. You can use Fisher-Yates algorithm to randomize your questions.

You could encapsulate the algorithm in an extension method like this:

public static class Extensions
{
   private static Random rnd = new Random();
   public static void Shuffle<T>(this IList<T> collection)
   {
       int n = collection.Count;
       while (n > 1)
       {
           n--;
           int k = rnd.Next(n + 1);
           T value = collection[k];
           collection[k] = collection[n];
           collection[n] = value;
       }
   }
}

Then on your LoadQuiz method, you may call the Shuffle extension method.

var questions = db.Questions.Find(Q_ID).ToList().Shuffle();

You may also take a look on this SO post

Community
  • 1
  • 1
Bon Macalindong
  • 1,310
  • 13
  • 20