前台:
<script src="../../Scripts/jquery-1.4.4.js" type="text/javascript"></script> <script src="../../Scripts/knockout.debug.js" type="text/javascript"></script> <script src="../../Scripts/knockout.mapping-latest.debug.js" type="text/javascript"></script> <div> --<p data-bind="text: Id"> </p> <ul> <li></li> </ul> </div> <h2> Demo</h2> <script type="text/javascript"> var data = {}; var viewModel = {}; $(function () { $.getJSON("http://localhost:9299/Home/GetAllReps?q=" + new Date(), {}, function (r) { data = r; }); ko.mapping.fromJS(data, viewModel); }); ko.applyBindings(viewModel); </script>
后台获取json的action:
[HttpGet] public ActionResult GetAllReps() { var gustes = new Guest { Id = 11, Body = "KnockoutJs is a wonderful javascript", Subject = "Knockoutjs...", CreatTime = DateTime.Now.AddDays(-1) }; return Json(gustes, JsonRequestBehavior.AllowGet); }
这样绑定运行不了,提示Id不能绑定,mapping插件改怎么正确的绑定呢?
欧呃