我是新手knockout.js。此外,我ama上層intermadiate在asp.net mvc 3.我真的想學習如何使用mvc 3剃鬚刀淘汰賽js?但下面的代碼不工作也返回給我空總價。沒有錯誤。請幫助感謝...如何在MVC 3 Razor中使用knockout.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);
}
}