我的第二个FluentNHibernate例子with Knockout
在上一篇我的第一个FluentNHibernate例子的基础上,我们用上knockoutjs
1用nuget添加knockoutjs包
2用nuget添加json.net包
3..在Index.cshtml中添加
<script src="~/Scripts/knockout-3.4.0.js"></script>
4.添加script在table后面
<script>
function ViewModel(items) {
var self = this;
self.items = items;
}
var viewmodel = new ViewModel(@Html.HtmlConvertToJson(Model));
ko.applyBindings(viewmodel);
</script>
5.修改Index.cshtml
<thead> <tr> <th>@Html.DisplayNameFor(model => model.Name)</th> <th>@Html.DisplayNameFor(model => model.Age)</th> <th>@Html.DisplayNameFor(model => model.Description)</th> </tr> </thead> <tbody data-bind="foreach:items"> <tr> <td data-bind="text:Name"></td> <td data-bind="text:Age"></td> <td data-bind="text:Description"></td> <td><a data-bind="attr:{href:'@Url.Action("Edit")/'+Id}" class="btn btn-primary">Edit</a> <a data-bind="attr:{href:'@Url.Action("Delete")/'+Id}" class="btn btn-danger">Delete</a></td> </tr> </tbody>
结果:

完整代码:
链接: http://pan.baidu.com/s/1o88FxQ6
密码: 74r7
这是一次knoutoutjs的尝试,不对之处望大家指正
浙公网安备 33010602011771号