我正在尝试将淘汰赛与我的 asp.net mvc 4 应用程序一起使用。这就是我的代码的样子。
<script>
var my = my || {}; //creating private namespace to avoid any conflicts other namespaces: my namespace
$(document).ready(function () {
////////////////view model testing////////////////////////////
// Define Main ViewModel; javascript Object Literals
////it is a workaround for moduler JS pattern including revealing js pattren
///it also uses KnockOut. end product ViewModel;
function teammemberModel() {
this.Id = ko.observable();
this.Title = ko.observable();
this.Name = ko.observable();
this.Email = ko.observable();
this.Nationality = ko.observable();
this.Sex = ko.observable();
};
my.viewModel = function () {
var teamMembers = ko.observableArray([]),
loadTeamMembers = function (projectId) {
$.ajax({
type: "GET",
url: "/Project/GetTeamMembers?projectId=" + projectId,
success: function (response) {
my.viewModel.teamMembers.removeAll();
$.each(response.results, function (x, team) {
my.viewModel.teamMembers.push(new teammemberModel()
.Id(team.Id)
.Title(team.Title)
.Name(team.UserName)
.Email(team.Email)
.Nationality(team.Nationality)
.Sex(team.Sex)
);
});
}
});
}
return {
teamMembers: teamMembers,
loadTeamMembers: loadTeamMembers
};
} ();
//Applies KO bindings
ko.applyBindings(my.viewModel);
my.viewModel.loadTeamMembers(6);
///////////////////////////////////////////////////
});
</script>
这是我的客户端 Knockout 基本视图模型的示例模块化 JS 实现。我的观点如下所示。
<table >
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th>Status</th>
<th>Created By</th>
<th>Created Date</th>
</tr>
</thead>
<tbody data-bind="foreach: teamMembers">
<tr>
<td data-bind="text: UserName"></td>
<td data-bind="text: Email"></td>
<td data-bind="text: Sex"></td>
<td data-bind="text: Title"></td>
<td data-bind="text: Nationality"></td>
</tr>
</tbody>
</table>
我可以在我的 ajax 调用中看到 json 数据被推送到 teamMembers ko.observableArray 中。此代码应该按照我尝试遵循的教程工作,但我没有在我的表中显示我的任何数据。有人可以指导我这段代码有什么问题以及为什么我的表格没有在这里呈现。谢谢