我是新手 knockout.js。我也是 asp.net mvc 3 中的上级中间人。我真的很想学习如何在 mvc 3 razor 中使用淘汰赛 js?但下面的代码不起作用也返回给我空的总价。没有错误。请帮忙谢谢...
模型:
public class GiftModel
{
public string Title { get; set; }
public double Price { get; set; }
}
看法:
@using System.Web.Script.Serialization;
@model IEnumerable<knockout1.Models.GiftModel>
@{
ViewBag.Title = "Index";
}
<script src="/Scripts/knockout-2.1.0.js" type="text/javascript"></script>
<script type="text/javascript">
var initialData = @(new JavaScriptSerializer().Serialize(Model));
var viewModel = {
gifts : ko.observableArray(initialData)
};
ko.applyBindings(viewModel);
</script>
<h2>Index</h2>
<p>You have asked for <span data-bind="text: gifts().length"> </span> gift(s)</p>
控制器:
public class TestController : Controller
{
//
// GET: /Test/
public ActionResult Index()
{
var initialState = new[] {
new GiftModel { Title = "Tall Hat", Price = 49.95 },
new GiftModel { Title = "Long Cloak", Price = 78.25 }
};
return View(initialState);
}
}