在线测试系统asp.net mvc

时间:2015-12-12 08:47:57

标签: javascript c# asp.net-mvc model-view-controller

我能够通过id显示问题,但不知道如何为不同的用户随机显示它们,并且每次用户登录新的随机组合时都会启动。 有人可以指导吗?

我的控制器: -

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");
    }`

测验库:

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;
    }

我的观点: -

    @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 个答案:

答案 0 :(得分:1)

你可以简单地使用Shuffle算法随机化你的问题。您可以使用Fisher-Yates算法随机化您的问题。

您可以将算法封装在这样的扩展方法中:

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;
       }
   }
}

然后在您的LoadQuiz方法上,您可以调用Shuffle扩展方法。

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

您还可以查看此SO post