如何在JavaScript Timer中调用Controller的动作

时间:2016-12-12 13:37:50

标签: javascript c# asp.net

这是我的新计时器:

         var eventDate = new Date(Date.parse(new Date) + 3600);

            function countdown() {

                var elapsed = Date.parse(eventDate) - Date.parse(new Date());

                var seconds = Math.floor((elapsed / 1000) % 60);
                var minutes = Math.floor((elapsed / 1000 / 60) % 60);

                minutes = (minutes < 10) ? "0" + minutes : minutes;
                seconds = (seconds < 10) ? "0" + seconds : seconds;

                return {
                    'minutes': minutes,
                    'seconds': seconds,
                    'total': elapsed
                }
            }

            function initializeClock() {
                var minutes = document.getElementById('minutes');
                var seconds = document.getElementById('seconds');

                var timeInterval = setInterval(function() {
                        minutes.innerHTML = countdown().minutes;
                        seconds.innerHTML = countdown().seconds;

                        if(minutes.equals(0) && seconds.equals(0))
                         {
                            timeInterval().stop();

                            $.ajax({
                                    url: "Finish/Test",
                                    type: 'POST',
                                    success: function (result) {
                                   if (result.success) {
                                  $('#htmlElement').html(result.htmlElement);
                                  }
                               }
                           }); 
                        }
                    },1000);
            }

            initializeClock();

    <h2>@ViewBag.Title</h2>
        <div id ='hmtlElement'>

....等等在html中

     [HttpPost]
            public ActionResult Finish(ExamineTest examineTest)
            {
                var question = examineTest.Question.ToList();

                _questions.AddRange(question);
                _testService.SaveSolvedTest(examineTest);

                return Json(new { success = true, htmlElement = RedirectToAction("Preview", "Test") });

            }




   [HttpGet]
        public ActionResult Preview()
        {
            SolvedTest solvedTest = _testService.GetSolvedTest();

            SolvedTestModel solvedTestModel = EntitySolvedTestConverter.ToModel(solvedTest);
            return View("Finish", solvedTestModel);
        }

1 个答案:

答案 0 :(得分:0)

你可以通过使用像@Bharat提到的ajax和json来做到这一点。这是一个可以调用控制器的ajax方法的代码片段。

function countdown(){
// checking your minute second logic
if(minute second equals 0)
    //var firstName = textbox where name is stored
    //var list = listbox where list is stored.
    var url = "Test/Finish?" + firstName + "&" + list;
    // url looks like Test/Finish?John&ListofElements
    $.ajax({
            url: url,
            type: 'POST',
            success: function (result) {
                if(result.success){
                    $('#htmlElement').html(result.htmlElement);
                }
            }
        });

}

上面你可以使用一些MVC路线作为网址。

控制器可能如下所示。

[HttpPost]
public ActionResult Finish(ExamineTest examineTest)
{
    var question = examineTest.Question.ToList();
    _questions.AddRange(question);
    _testService.SaveSolvedTest(examineTest);

    SolvedTest solvedTest = _testService.GetSolvedTest();
    SolvedTestModel solvedTestModel = EntitySolvedTestConverter.ToModel(solvedTest);

    return Json(new { success = true, htmlElement = View("Finish", solvedTestModel)});
}

然后是你的逻辑。当分钟和秒等于0时,只需调用它。

<body>
<h2>@ViewBag.Title</h2>
<div id='htmlElement'>
<!-- ..... stuff .... -->
<!-- end of html form -->
</div>