我是淘汰赛的新手,我有一点问题。我有 ASP.Net MVC 4 应用程序、剃刀视图和淘汰视图模型。我使用 web api 控制器来获取数据,我无法用它初始化 ko.observableArray。
获取数据的控制器函数:
public IEnumerable<TaskModel> GetTasks()
{
var tasks = new[] { new TaskModel { Name = "asd", Deadline = DateTime.Now, Id = new Guid() }, new TaskModel { Name = "asdfasdf", Deadline = DateTime.Now, Id = new Guid() } };
return tasks;
}
淘汰视图模型(对控制器的调用正确,数据变量不为空)
function TasksList() {
var self = this;
self.tasks = ko.observableArray([]);
self.load = function() {
$.ajax({
type: "GET",
url: "http://localhost:11701/api/TaskApi",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
self.tasks(data);
},
error: function(error) {
alert(error.status + "<--and--> " + error.statusText);
}
});
};
}
var tasksList = new TasksList();
tasksList.load();
ko.applyBindings(tasksList);
看法
@{
ViewBag.Title = "Index";
}
@Scripts.Render("~/Scripts/jquery-1.8.3.js")
@Scripts.Render("~/Scripts/knockout-2.2.1.debug.js")
@Scripts.Render("~/Scripts/knockout.mapping-latest.debug.js")
@Scripts.Render("~/Scripts/Models/TasksList.js")
<h2>Tasks</h2>
<table>
<tr>
<th>Name
</th>
<th>Priority
</th>
<th>Deadline
</th>
<th>CreatedOn
</th>
<th></th>
</tr>
<tbody data-bind="foreach: tasks" >
<tr>
<td data-bind="text: Name"></td>
<td data-bind="text: Priority"></td>
<td data-bind="text: Deadline"></td>
<td data-bind="text: CreatedOn"></td>
</tr>
</tbody>
</table>
我尝试了 ko.mapping.fromJS 和 ko.mapping.fromJSON ,但它仍然不起作用;知道我做错了什么吗?提前致谢