无法在MVC5中将参数从视图传递到控制器

时间:2017-11-05 21:40:29

标签: c# razor asp.net-mvc-5

我正在尝试使用ActionLink将ViewModel的实例从我的视图传递给控制器​​。但是,ViewModel在到达控制器时始终为null。

ActionLink的:

@Html.ActionLink("Begin Test", "TakeTest", new { vm = Model }, new { @class = "btn btn-default" })

控制器:

 public ActionResult TakeTest(QuizViewModel vm) {
        ViewBag.Title = vm.vmTest.TestName;

        string userId = User.Identity.GetUserId();
        vm.Student = db.Users.SingleOrDefault(s => s.Id == userId);

        vm.Attempt = db.Attempts.SingleOrDefault(a => a.Student == vm.Student && a.Test == vm.vmTest);
        if (vm.vmTest.AttemptsAllowed > 0 && vm.Attempt.Count >= vm.vmTest.AttemptsAllowed) {
                return View();
        }
        vm.Attempt.Count++;
        if (vm.QuestionList == null) {
            vm.QuestionList = db.Questions.Where(q => q.TID == vm.vmTest.ID).ToList();
        }

        Random r = new Random();
        int randQid = r.Next(0, vm.QuestionList.Count - 1);
        vm.ShowQuestion = vm.QuestionList[randQid];
        vm.QuestionList.Remove(vm.ShowQuestion);
        vm.Asets = db.AnswerSets.Where(a => a.Question == vm.ShowQuestion).ToList();


        return View(vm);

    }

0 个答案:

没有答案
相关问题