I just look in knockout.js with the MVC-Web-API and I am trying to create a Hello World page that will update the time on the page every 5 seconds. It makes a call every 5 seconds, I see it in my controller (breakpoint), but nothing is displayed on the screen.
UPDATE: I am still working on this, and now I have established that I return data from the server, the call is made by the controller every 5 seconds, and it returns the JSON I need (warnings show this) however, nothing is displayed on the span element on the page yet.
I really need to use the mapping function, as I am developing a larger website that has a model with more than 50 properties and doesnβt particularly want to go through and display them separately in the viewmodel.
I have included my code below.
<span data-bind="text: TimeString"></span> <script type="text/javascript"> var viewModel; var getUpdates = setInterval(function () { $.getJSON( "/Values/Get", {}, function (model) { alert(model.TimeString); ko.mapping.fromJS(model, viewModel); }); }, 5000); $(document).ready( function () { $.getJSON( "/Values/Get", {}, function (model) { var viewModel = ko.mapping.fromJS(model); alert(model.TimeString); ko.applyBindings(viewModel); }); }); function bindViewModel(model) { ko.applyBindings(model); }
public class HelloWorldModel { public DateTime TimeDT { get; set; } public String TimeString { get; set; } } public class ValuesController : Controller { public HelloWorldModel Model = new HelloWorldModel(); [System.Web.Mvc.AcceptVerbs(HttpVerbs.Get)] public JsonResult Get() { Model.TimeDT = DateTime.Now; Model.TimeString = Model.TimeDT.ToString("HH:mm:ss"); return Json(Model, JsonRequestBehavior.AllowGet); }
source share