Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout MVC - ServerAction call not executing

I am trying to build a website that is hosted on Azure as a free website, it is using MVC4, with KnockoutMVC. The website is updating a table every 4 seconds from the controller. The code executes without problem locally, however when I deploy it to the Azure website, it does not process updates.

I now have started a completely new MVC 4 project, with some simple functions using knockout, showing the current time, and updating this every .5 of a second from the controller, it is using the same structure as the javascript I have written for the proper website. It is giving the same problem, and it appears as if the ko.ServerAction call is halting the javascript function.

View

@using PerpetuumSoft.Knockout
@model MVCHelloWorld.Models.HelloWorldModel
@{
  var ko = Html.CreateKnockoutContext();
 }
<script type="text/javascript">
 function startTime() {
    var today = new Date();
    var h = today.getHours();
    var m = today.getMinutes();
    var s = today.getSeconds();
    // add a zero in front of numbers<10
    m = checkTime(m);
    s = checkTime(s);
    document.getElementById('time').innerHTML = h + ":" + m + ":" + s;

    @ko.ServerAction("Index", "HelloWorld");

    t = setTimeout(function() { startTime(); }, 500);
}

function checkTime(i) {
    if (i < 10) {
        i = "0" + i;
    }
    return i;
}  
</script>

<body onload="startTime()">
   <h2>Hello World - 2</h2>
   <div id="time"></div>

   <label>Knockout time</label>
   @ko.Html.Span(m => m.Time)
</body>

@ko.Apply(Model)

Controller

namespace MVCHelloWorld.Controllers
{
  public class HelloWorldController : BaseController
  {

    public HelloWorldModel model = new HelloWorldModel();

    public ActionResult Index()
    {
        GetTimeDoCalculation();

        return View();
    }

    public void GetTimeDoCalculation()
    {
        model.Time = DateTime.Now.ToString("H:mm:ss");
    }
 }
}

Model

namespace MVCHelloWorld.Models
{
public class HelloWorldModel
  {
     public string Time { get; set; }
  }
}
like image 879
Ben Sharpe Avatar asked Mar 08 '26 17:03

Ben Sharpe


1 Answers

Try adding this to the body of your view:

<script type="text/javascript">
    @* Replace 4000 with the timeout, in milliseconds *@
    window.setInterval(startTime, 4000)
</script>

Your entire view will look like this:

@using PerpetuumSoft.Knockout
@model MvcApplication2.Models.HelloWorldModel
@{
    var ko = Html.CreateKnockoutContext();
 }
<script type="text/javascript">
    function startTime() {
        var today = new Date();
        var h = today.getHours();
        var m = today.getMinutes();
        var s = today.getSeconds();
        // add a zero in front of numbers<10
        m = checkTime(m);
        s = checkTime(s);
        document.getElementById('time').innerHTML = h + ":" + m + ":" + s;

        @ko.ServerAction("Index", "HelloWorld");

        t = setTimeout(function() { startTime(); }, 500);
    }

    function checkTime(i) {
        if (i < 10) {
            i = "0" + i;
        }
        return i;
    }  
</script>

<body onload="startTime()">
   <h2>Hello World - 2</h2>
   <div id="time">

       <script type="text/javascript">
           @* Replace 4000 with the timeout, in milliseconds *@
           window.setInterval(startTime, 4000)
       </script>

       <label>Knockout time</label>
   @ko.Html.Span(m => m.Time)

   </div>
</body>

@ko.Apply(Model)
like image 171
Anon Avatar answered Mar 11 '26 10:03

Anon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!